I have a variable that become value from NSUserDefaults.standardUserDefaults()
var GiftCount = NSUserDefaults.standardUserDefaults().valueForKey("Gift") as! Int
And i have a function named setGiftCount()...
I need call this function when variable GiftCount has changed... How to do it?
First
NSUserDefaults.standardUserDefaults().addObserver(self, forKeyPath: "Gift", options: NSKeyValueObservingOptions.New, context: nil)
Second
deinit {
NSUserDefaults.standardUserDefaults().removeObserver(self, forKeyPath: "Gift")
}
Third
override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
setGiftCount()
}