Reputation: 1067
I have a group of buttons, and when two buttons are pressed, then an NSUserDefault is triggered,but I need an observer in the ViewController to do some animation.
Is this possible in swift ? Could I use DidSet: and WillSet: or is there an other option ?
Upvotes: 1
Views: 1098
Reputation: 10286
The easiest way to do that is to combine both user defaults and animations on a computed variable as follow:
var defaultName:String{
get {
var returnValue: NSString? = NSUserDefaults.standardUserDefaults().objectForKey("defaultName") as? NSString
if returnValue == nil //Check for first run of app
{
returnValue = ""
}
return returnValue!
}
set (newValue) {
NSUserDefaults.standardUserDefaults().setObject(newValue, forKey: "defaultName")
NSUserDefaults.standardUserDefaults().synchronize()
// start your animations here
}
}
Next on button click just change the defaultName value so both newValue will be stored and animations will trigger
Upvotes: 6