TheDoge TheDoge - 1 year ago 74
Swift Question

How to save time interval in NSUserDefaults

I am trying to save

by doing this in a playground-

var date2 = Double(NSDate.timeIntervalSinceReferenceDate())
NSUserDefaults.standardUserDefaults().setDouble(date2, forKey: "timeInterval")

And I am trying to retrieve this date by doing this-


But when I print this, I get the output as 0. How can I change that to get the correct time interval as a double? Thanks in advance.

Answer Source

Try to save it as object for key maybe,

NSUserDefaults.standardUserDefaults().setObject(date2, forKey:"yourKey")
NSUserDefaults.standardUserDefaults().objectForKey("yourKey") as! NSDate 


You can't access to NSUsersDefaults in the Playground environnement, You can see why here

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download