gpt4 book ai didi

swift - 带有解码器的 swift 中的 RLMArray : Ambiguous reference to member error

转载 作者:行者123 更新时间:2023-11-30 11:17:13 26 4
gpt4 key购买 nike

我想在混合 Objective-C 和 Swift 应用程序中使用 Realm 并与 Codable 一起使用,并且 Realm 对象可以导出到 Objective-C ;

class Person2 : RLMObject,Decodable {
@objc dynamic var name = ""
convenience init(_ name:String) {
self.init()
self.name = name
}
}

class RepairShop2 : RLMObject,Decodable {
@objc dynamic var name = ""
@objc dynamic var contact:Person2?
@objc dynamic var persons = RLMArray<Person2>(objectClassName: Person2.className())

private enum RepairShop2CodingKeys: String, CodingKey {
case name
case contact
case persons
}

convenience init(name: String, contact: Person2, persons: RLMArray<Person2>) {
self.init()
self.name = name
self.contact = contact
self.persons = persons
}

convenience required init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: RepairShop2CodingKeys.self)
let name = try container.decode(String.self, forKey: .name)
let contact = try container.decode(Person2.self, forKey: .contact)
let personArray = try container.decode(RLMArray<AnyObject>, forKey: .persons)
// this line error: Ambiguous reference to member 'decode(_:forKey:)'**

let persons = RLMArray<Person2>(objectClassName: Person2.className())
persons.addObjects(personArray)
self.init(name: name, contact: contact, persons: persons)
}
}

let personArray = try container.decode(RLMArray<AnyObject>, forKey: .persons)
// this line error: Ambiguous reference to member 'decode(_:forKey:)'**

RLMArray.self我也尝试过,失败

如何编写RLMArray的解码类型?

最佳答案

RLMRealm 不符合 Decodable,因此您无法立即将其解析为 RLMRealm。相反,尝试类似的方法:

let persons = RLMArray<Person2>(objectClassName: Person2.className())
persons.addObjects(try container.decode([Person2].self, forKey: .persons) as NSFastEnumeration)

作为旁注。将不同的 Realm 混合到一个模型中是个坏主意,它可能会在以后产生影响。

关于swift - 带有解码器的 swift 中的 RLMArray : Ambiguous reference to member error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51633392/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com