Codable class 不符合协议 'Decodable'
Codable class doesn't conform to protocol 'Decodable'
尝试在结构中添加枚举 Codingkeys,但它显示错误 Codable doesn't conform Decodable。
为什么我会收到一致的可解码错误?我应该分开结构吗?
struct Model: Codable {
let aps: Aps
let link: String?
let checkAction: String?
enum CodingKeys: String, CodingKey {
case aps ,link, alert,sound,title,body
case checkAction = "gcm.notificaiton.check_action"
}
struct Aps: Codable {
let alert: Alert
let sound: String?
struct Alert: Codable {
let title: String?
let body: String?
}
}
}
是否必须像下面这样分隔结构?
struct FCMModel: Codable {
let aps: Aps
let link: String?
let checkAction: String?
enum CodingKeys: String, CodingKey {
case aps ,link
case checkAction = "gcm.notificaiton.check_action"
}
}
struct Aps: Codable {
let alert: Alert
let sound: String?
struct Alert: Codable {
let title: String?
let body: String?
}
}
没有必要将结构分开。发生错误是因为您在 CodingKeys
枚举中添加了太多键。如果您只保留必需的,那么它将起作用:
struct Model: Codable {
let aps: Aps
let link: String?
let checkAction: String?
enum CodingKeys: String, CodingKey {
case aps ,link
case checkAction = "gcm.notificaiton.check_action"
}
struct Aps: Codable {
let alert: Alert
let sound: String?
struct Alert: Codable {
let title: String?
let body: String?
}
}
}
alert
、sound
等不是Model
的编码键。它们是Aps
的编码键。不必在 Aps
中指定它们,因为它们与 属性 名称相同。
尝试在结构中添加枚举 Codingkeys,但它显示错误 Codable doesn't conform Decodable。
为什么我会收到一致的可解码错误?我应该分开结构吗?
struct Model: Codable {
let aps: Aps
let link: String?
let checkAction: String?
enum CodingKeys: String, CodingKey {
case aps ,link, alert,sound,title,body
case checkAction = "gcm.notificaiton.check_action"
}
struct Aps: Codable {
let alert: Alert
let sound: String?
struct Alert: Codable {
let title: String?
let body: String?
}
}
}
是否必须像下面这样分隔结构?
struct FCMModel: Codable {
let aps: Aps
let link: String?
let checkAction: String?
enum CodingKeys: String, CodingKey {
case aps ,link
case checkAction = "gcm.notificaiton.check_action"
}
}
struct Aps: Codable {
let alert: Alert
let sound: String?
struct Alert: Codable {
let title: String?
let body: String?
}
}
没有必要将结构分开。发生错误是因为您在 CodingKeys
枚举中添加了太多键。如果您只保留必需的,那么它将起作用:
struct Model: Codable {
let aps: Aps
let link: String?
let checkAction: String?
enum CodingKeys: String, CodingKey {
case aps ,link
case checkAction = "gcm.notificaiton.check_action"
}
struct Aps: Codable {
let alert: Alert
let sound: String?
struct Alert: Codable {
let title: String?
let body: String?
}
}
}
alert
、sound
等不是Model
的编码键。它们是Aps
的编码键。不必在 Aps
中指定它们,因为它们与 属性 名称相同。