也许这个问题比我看上去的要笼统,但是我想确保在出现某些问题的情况下,能显示出我的完整背景。
class Singleton: NSObject {
static let sharedInstance = Singleton()
@objc dynamic var aProperty = false
func updateDoesntWork() {
aProperty = !aProperty
}
func updateDoesWork() {
Singleton.sharedInstance.aProperty = !aProperty
}
}
Singleton.sharedInstance.addObserver(self, forKeyPath: #keyPath(Singleton.aProperty), options: [.new], context: nil)
observeValue()
方法:override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
NSLog("observeValue(forKeyPath: \(String(describing:keyPath)), of: \(String(describing:object)), change: \(String(describing:change)), context:\(String(describing:context)))")
}
现在,如果我拨打Singleton.sharedInstance.updateDoesntWork()
,则不会获得aProperty
中更改的日志条目。属性已更改(我在调试器中对此进行了验证),只是没有通知发送。
如果我调用Singleton.sharedInstance.updateDoesWork()
,那么一切都会按我的预期进行-当然,该属性也已更改,但最重要的是,这次通知了观察者更改(打印日志条目) )。
对于我来说,为了使KVO正常工作,我需要完整的Singleton.sharedInstance.aProperty
而不是aProperty
毫无意义。我想念什么?
答案 0 :(得分:0)
我认为您在将“ var”用于单例时遇到麻烦。您可以考虑使用以下代码段创建一个单例并初始化一些值,包括该单例专用的观察值:
class Singleton: NSObject {
static private var sharedInstanceObserver : NSKeyValueObservation!
static let sharedInstance: Singleton = {
let sInstance = Singleton()
sharedInstanceObserver = sInstance.observe(\Singleton.aProperty, options: .new) { st, value in
print(st, value)
}
return sInstance
}()
@objc dynamic var aProperty = false
func updateDoesntWork() {
aProperty = !aProperty
}
func updateDoesWork() {
Singleton.sharedInstance.aProperty = !aProperty
}
}