【发布时间】:2021-12-29 19:19:46
【问题描述】:
我正在尝试对数据进行编码和确定,并且编码工作得很好,但是当我使用解码器解码数据时,我的解码器函数给我一个错误。请查看我的代码,让我知道我应该怎么做才能正确解码数据。调试后我发现的问题是解码器块在 switch 旁边没有继续进行,而是在 type 上返回并且错误为 , DecodingKey type not found。
type = try container.decode(WorkoutType.self, forKey: .type) 这是当我想解码数据时它不会继续进行的行。
这是我的代码
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")
}
}
}
这是我在调用 init(from decoder: Decoder) 时遇到的错误 keyNotFound(CodingKeys(stringValue: "type", intValue: nil), Swift.DecodingError.Context(codingPath: [_JSONKey(stringValue: "Index 0", intValue: 0)], debugDescription: "No value associated with key CodingKeys(stringValue :“类型”,intValue:无)(“类型”)。”,基础错误:无)) keyNotFound(CodingKeys(stringValue: "type", intValue: nil), Swift.DecodingError.Context(codingPath: [_JSONKey(stringValue: "Index 0", intValue: 0)], debugDescription: (lldb)
【问题讨论】:
-
显示导致错误的 JSON...
-
请看,我已经添加了json文件
-
问题出在此处。 type = try container.decode(WorkoutType.self, forKey: .type)。 .因为它没有进入我使用 switch 语句的下一步,因为它没有找到编码键“类型”
-
那不是一个有效的 JSON 文件。这看起来可能是 Xcode 打印到日志的内容,但它绝对不是真正的 JSON。
-
除非我完全误读了这一点,否则解码器无法解码
type的原因是编码器没有对其进行编码。我在encode(to:)的任何地方都看不到try container.encode(type, forKey: .type)
标签: ios json swift codable decodable