gpt4 book ai didi

swift - Realm 对象的自定义 setter/willSet

转载 作者:搜寻专家 更新时间:2023-11-01 06:07:06 29 4
gpt4 key购买 nike

每当我设置一个 Realm 对象的属性时,我想触发代表我远程后端上的对象的另一个对象的更改。

我想知道这是否仍然是推荐的最佳实践:

https://github.com/realm/realm-cocoa/issues/870#issuecomment-54543539

我试图做的,但因为它干扰了 Realm 而不起作用:

dynamic var name: String = "" {
willSet(newValue) {
self.name = newValue
self.widgetRemote?.name = newValue
}
}

最佳答案

是的,realm/realm-cocoa#870 中建议的解决方法仍然是实现这一目标的最佳方式。对于您的情况,您需要执行以下操作:

@objc private dynamic var backingName = ""

var name : String {
get {
return backingName
}
set(newValue) {
backingName = newValue
widgetRemote?.name = newValue
}
}

override class func ignoredProperties() -> [String] {
return ["name"]
}

关于swift - Realm 对象的自定义 setter/willSet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34773597/

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