-4

I am trying to save the time once the "user" reachs the Limit. So the limit is 10 for example and once he reachs this limit, i want to save the current time. Then he has to wait 1 hour to continue playing. I started doing this, but I already get an error, when I try this:

var CurrentTime = NSDate()
CurrentTime = NSUserDefaults.standardUserDefaults()

Error:

Cannot assign value of type 'NSUserDefaults' to type 'NSDate'

It seems like swift cannot save a 'NSDate' as a 'NSUserDefault'.

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Ketarr
  • 15
  • 2
  • 6

1 Answers1

5

NSUserDefaults.standardUserDefaults() is a kind of dictionary, you dont use the dictionary as a date, you set a date value to one of its keys:

 let userDefaults = NSUserDefaults.standardUserDefaults()    

 // save to user defaults
 userDefaults.setObject(NSDate(), forKey: "LimitReachedOnDate")

 // retrieve from user defaults
 let limitDate = userDefaults.objectForKey("LimitReachedOnDate") as? NSDate ?? NSDate.distantFuture()
Alain T.
  • 40,517
  • 4
  • 31
  • 51