我面临一个问题,我无法使 RealmOptional 与带有 json 解码器的 swift 新 Codable 功能兼容。
考虑以下 Realm 对象。
class School: Object, Codable {
@objc dynamic var id: Int64 = 0
@objc dynamic var name: String?
var numberOfStudents = RealmOptional<Int64>()
var classes = List<Class>()
enum CodingKeys: String, CodingKey {
case id
case name
case numberOfStudents
case classes
}
}
class Class: Object, Codable {
var name: String?
var numberOfStudents = RealmOptional<Int64>()
}
在这里,我们可以将类声明为 Codable,因为我在这个 gist的帮助下为 RealmOptinal 编写了一个扩展。但问题是当解码器解码 json 时。
考虑这个json
let jsonData = """
[
"id": 1234,
"name": "Shreesha",
"numberOfStudents": nil,
"classes": {
"name": "Class V",
"numberOfStudents": 12
}
]
""".data(using: .utf8)!
在这个 json 中,所有数据都被传递,并且这与代码完美解码。
let decoder = JSONDecoder()
let decoded = try! decoder.decode(School.self, from: jsonData)
但是,如果我numberOfStudents
从应该是 RealmOptional 对象的 json 数据中删除密钥,它将引发错误并且不会解码,因为 RealmOptional 不是 swift 可选的,因此解码器认为 json 数据中应该有一个密钥。如果JSONDecoder
json 中不存在密钥并且该属性被声明为可选,则它不会尝试解码。它只是跳到其他键。
到目前为止,我没有覆盖初始化程序,因为我们拥有RealmOptional
RealmLists
等的所有支持扩展。但是现在我必须覆盖init(from decoder: Decoder)
它才能手动解码它,并且 Realm 模型中有超过50 个属性(你知道我的意思)。
如果我们覆盖初始化器,我觉得使用没有意义,JSONDecoder
因为与使用 JSONDecoder 相比,手动工作更多。
required convenience init(from decoder: Decoder) throws {
self.init()
let container = try decoder.container(keyedBy: CodingKeys.self)
id = try container.decodeIfPresent(Int64.self, forKey: .id) ?? 0
name = try container.decodeIfPresent(String?.self, forKey: .name) ?? ""
numberOfStudents = try container.decodeIfPresent(RealmOptional<Int64>.self, forKey: .numberOfStudents) ?? RealmOptional<Int64>()
let classesArray = try container.decode([Class].self, forKey: .classes)
classes.append(objectsIn: classesArray)
}
所以有人可以建议我使 RealmOptional 兼容的替代解决方案,JSONDecoder
这样我们就不必覆盖初始化程序。