I have the following code aiming to catch the event of a NSUserDefaults value changing for a particular key.
[[NSUserDefaults standardUserDefaults] addObse
Swift Version:
func setUserDefaultsListener(){
NSUserDefaults.standardUserDefaults().addObserver(self, forKeyPath: "keyPath", options: NSKeyValueObservingOptions.New, context: nil)
}
override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer) {
if keyPath == "keyPath" {
//Do something
}
}
deinit {
NSUserDefaults.standardUserDefaults().removeObserver(self, forKeyPath: "keyPath")
}