Swift作为Codable(可解码和可编码)协议的这种特性非常有用 . 但我发现了这样的问题:让我们让家长符合Codable:
class Parent: Codable {
var name: String
var email: String?
var password: String?
}
好吧,那个类符合Codable协议“从框中”,你不需要编写任何初始化器,它可以从JSON初始化,如:{“name”:“John”,“email”:“johndoe @ yahoo.com“,”“密码”:}但是让我们说我们需要其他类,Child继承自Parent并且符合Codable:
class Child: Parent {
var token: String
var date: Date?
}
所以类Child必须符合Codable符合Parent,但类Child的属性不会正确地从JSON初始化 . 我发现的决定是自己写一些可用于课堂的Codable东西,比如:
class Child: Parent {
var token: String
var date : Date?
enum ChildKeys: CodingKey {
case token, date
}
required init(from decoder: Decoder) throws {
try super.init(from: decoder)
let container = try decoder.container(keyedBy: ChildKeys.self)
self.token = try container.decode(String.self, forKey: .token)
self.date = try container.decodeIfPresent(Date.self, forKey: .date)
}
override func encode(to encoder: Encoder) throws {
try super.encode(to: encoder)
var container = encoder.container(keyedBy: ChildKeys.self)
try container.encode(self.token, forKey: .token)
try container.encodeIfPresent(self.date, forKey: .date)
}
}
但我觉得这不对,我错过了什么吗?如何在不编写所有内容的情况下使类符合Codable的类?
1 回答
这是一篇很好的博客文章,其中包含对您问题的回答:source
Scroll down to inheritance and you'll see the following:
假设我们有以下类:
我们通过继承Person类来获得Codable一致性,但是如果我们尝试编码Employee实例会发生什么?
这不是预期的结果,所以我们必须添加这样的自定义实现:
子类的相同故事:
结果将是:
这也不是预期的结果,所以我们在这里通过调用 super 来使用 inheritance .
这最终给了我们从一开始就想要的东西:
如果你对扁平化的结果不满意,那么就有一个关于如何避免这种情况的提示 .
给写博客文章的人以及我的感谢所有的积分 . 希望它也能帮到你,欢呼!