Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import Foundation
class Singleton: NSObject {
static let sharedInstance = Singleton()
@objc dynamic var aProperty = false
func updateDoesntWork() {
aProperty = !aProperty
}
func updateDoesWork() {
Singleton.sharedInstance.aProperty = !aProperty
}
}
class Observer: NSObject {
override init() {
super.init()
Singleton.sharedInstance.addObserver(self, forKeyPath: #keyPath(Singleton.aProperty), options: [.new], context: nil)
}
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
print("observeValue(forKeyPath: \(String(describing:keyPath)), of: \(String(describing:object)), change: \(String(describing:change)), context:\(String(describing:context)))")
}
}
let observer = Observer()
Singleton.sharedInstance.updateDoesWork()
Singleton.sharedInstance.updateDoesntWork()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.