I am trying to encode and decide the data and encoding working perfectly fine but when i use Decoder to decode the data , my decoder function is throwing me an error . Please look at my code and let me know what should i do in order to properly decode the data . The issue after debugging I found is decoder block does not proceed further next to switch , instead on type it returns and error with , DecodingKey type not found.
type = try container.decode(WorkoutType.self, forKey: .type) this is the line where it does not proceed further when i wanna decode the data.
here is my code
struct OverviewWorkout : Codable {
enum WorkoutType: String , Codable {
case workout
case coach
case bodyArea
case challenge
case title
case group
case trainer
}
enum WorkoutsData {
case workout(Workout)
case challenge(Workout)
case group([Workout])
case trainer([Trainer])
case bodyArea([Workout])
case coach(CoachInstruction)
case title(Title)
}
var type: WorkoutType
var data : WorkoutsData
init(from decoder: Decoder) throws {
print("decoder called")
let container = try decoder.container(keyedBy: CodingKeys.self)
type = try container.decode(WorkoutType.self, forKey: .type)
switch type {
case .workout, .challenge:
let data = try container.decode(Workout.self, forKey: .data)
self.data = .workout(data)
case .coach:
let data = try container.decode(CoachInstruction.self, forKey: .data)
self.data = .coach(data)
case .bodyArea:
let data = try container.decode([Workout].self, forKey: .data)
self.data = .bodyArea(data)
case .title:
let data = try container.decode(Title.self, forKey: .data)
self.data = .title(data)
case .group:
let data = try container.decode([Workout].self, forKey: .data)
self.data = .group(data)
// trainer data
case .trainer:
let data = try container.decode([Trainer].self, forKey: .data)
self.data = .trainer(data)
}
print("decodable called")
}
private enum CodingKeys: String, CodingKey {
case type,data
}
}
extension OverviewWorkout {
struct Title: Codable {
let title: String
}
}
extension OverviewWorkout {
func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
print(container)
switch data {
case .workout(let workout):
try container.encode(workout, forKey: .data)
case .bodyArea(let bodyarea):
try container.encode(bodyarea, forKey: .data)
case .title(let title):
try container.encode(title, forKey: .data)
case .coach(let coach):
try container.encode(coach, forKey: .data)
case .trainer(let trainer):
try container.encode(trainer, forKey: .data)
case .group(let group):
try container.encode(group, forKey: .data)
default:
print("out of range")
}
}
}
here is the error I am having whenever init(from decoder: Decoder) throws is called keyNotFound(CodingKeys(stringValue: "type", intValue: nil), Swift.DecodingError.Context(codingPath: [_JSONKey(stringValue: "Index 0", intValue: 0)], debugDescription: "No value associated with key CodingKeys(stringValue: "type", intValue: nil) ("type").", underlyingError: nil)) keyNotFound(CodingKeys(stringValue: "type", intValue: nil), Swift.DecodingError.Context(codingPath: [_JSONKey(stringValue: "Index 0", intValue: 0)], debugDescription: (lldb)
typeis that the encoder isn't encoding it. I can't see atry container.encode(type, forKey: .type)anywhere inencode(to:)