I have a project with an UIDatePicker
.
I want that if I change my UIDatePicker
value, for example at 9:23, when I restart my app value is still 9:23 and not the default value. How can I do that?
Here is my code:
@IBOutlet var myDatePicker: UIDatePicker!
var datePicker = /* Get Your Date Picker from somewhere */
// Store value using User Defaults
let currentDate = datePicker.date
NSUserDefaults.standardUserDefaults().setObject(currentDate, forKey: "Current-Date")
// Retrieve Value using User Defaults
if let date = NSUserDefaults.standardUserDefaults().objectForKey("Current- Date") as? NSDate {
datePicker.setDate(date, animated: true)
}
I tried with standard userDefaults
method but not worked.
Your key is not the same "Current-Date" != "Current- Date". That is why it is not working :
Use a constant to avoid this error:
Hi I prepared for you some snipped code:
Should works fine :)