[英]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
中指定它们,因为它们与属性名称相同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.