我有一个符合 Decodable 协议(protocol)(从 API 获取数据)的类,我想将它保存在 Realm 数据库中。当我的属性之一是数组(列表)时出现问题。它说Cannot automatically synthesize Decodable because List<Item> does not conform to Decodable
绕过这个问题的最好方法是什么? Realm 仅支持基本类型的数组。
这是我的类(class):
class PartValue: Object, Decodable {
@objc dynamic var idetifier: Int = 0
let items = List<Item>()
}
最佳答案
使用 Swift 4.1 中实现的期待已久的条件一致性,您可以简单地声明 List
以符合 Decodable
,以防其 Element
符合可解码
。
extension List: Decodable where List.Element: Decodable {
public convenience init(from decoder: Decoder) throws {
self.init()
var container = try decoder.unkeyedContainer()
let array = try container.decode(Array<Element>.self)
self.append(objectsIn: array)
}
}
为了使这项工作适用于您的特定情况,您需要确保 Item
也符合 Decodable
。
如果您还需要 Encodable
一致性,只需扩展 List
即可支持它。
extension List: Encodable where List.Element: Encodable {
public func encode(to encoder: Encoder) throws {
var container = encoder.unkeyedContainer()
try container.encode(contentsOf: Array(self))
}
}
关于arrays - 使用 Decodable 在 Realm 中保存对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49926957/