What's the optimum way of storing an NSDate in NSUserDefaults?

前端 未结 4 775
旧巷少年郎
旧巷少年郎 2020-11-28 21:28

There\'s two ways of storing an NSDate in NSUserDefaults that I\'ve come across.

Option 1 - setObject:forKey:

// Set
NSDate *myDate = [NSDate date]         


        
4条回答
  •  北荒
    北荒 (楼主)
    2020-11-28 22:04

    For option #1, I don't believe a date formatter is involved. Possibly under the hood, but I imagine it's not broken. The date is stored in ISO 8601 form.

    For option #2, use -setDouble:forKey: and -doubleForKey instead of the float-based versions. That might be the cause of your precision errors.

提交回复
热议问题