gpt4 book ai didi

swift - Realm swift update List 写入每个运行时

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

我的对象

   class RealmConversation: Object {

@objc dynamic var id = ""
var show = List<Show_id>()
var show_ids: [String] {
get {
return show.map { $0.show_id }
}
set {
show.removeAll()
show.append(objectsIn: newValue.map {Show_id(value: [$0])})
}
}
override class func ignoredProperties() -> [String] {
return ["show_ids"]
}
override static func primaryKey() -> String? {
return "id"
}
}

class Show_id:Object {
@objc dynamic var show_id = ""
}

附加 View Controller

 newSpecimen.show_ids = ["id1", "id2", "id3"]
realm.add(newSpecimen, update: true)
self.realmconversation = newSpecimen

结果是运行时总是增加 x2 show_ids如何更新字符串列表项

运行时1
enter image description here
运行时2
enter image description here

最佳答案

If you don’t want to save a field in your model to its Realm, override Object.ignoredProperties(). Realm won’t interfere with the regular operation of these properties; they’ll be backed by ivars, and you can freely override their setters and getters. Realm documentation

为此,在添加新对象之前,您应该检查 show_ids 是否包含您要添加的 id。

关于swift - Realm swift update List 写入每个运行时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48088865/

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