使用 Decodable 在 Realm 中保存对象数组
Saving array of objects in Realm with Decodable
我有一个符合 Decodable 协议的 class(从 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
符合 [=13] =].
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))
}
}
Dávid 的解决方案完全不适合我。我不得不通过将 decoder.unkeyedContainer()
替换为 decoder.singleValueContainer()
来调整解决方案,下面是解决方案。
extension List: Decodable where List.Element: Decodable {
public convenience init(from decoder: Decoder) throws {
self.init()
let container = try decoder.singleValueContainer()
let array = try container.decode([Element].self)
self.append(objectsIn: array)
}
}
我有一个符合 Decodable 协议的 class(从 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
符合 [=13] =].
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))
}
}
Dávid 的解决方案完全不适合我。我不得不通过将 decoder.unkeyedContainer()
替换为 decoder.singleValueContainer()
来调整解决方案,下面是解决方案。
extension List: Decodable where List.Element: Decodable {
public convenience init(from decoder: Decoder) throws {
self.init()
let container = try decoder.singleValueContainer()
let array = try container.decode([Element].self)
self.append(objectsIn: array)
}
}