Created
January 29, 2019 13:30
-
-
Save rnapier/4500b87cd3a253020be56e8bd03f54d3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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