There's two ways of storing an NSDate in NSUserDefaults that I've come across.
Option 1 - setObject:forKey:
// Set
NSDate *myDate = [NSDate date];
[[NSUserDefaults sharedUserDefaults] setObject:myDate forKey:@"myDateKey"];
// Get
NSDate *myDate = (NSDate *)[[NSUserDefaults sharedUserDefaults] objectForKey:@"myDateKey"];
Option 2 - timeIntervalSince1970
// Set
NSDate *myDate = [NSDate date];
NSTimeInterval myDateTimeInterval = [myDate timeIntervalSince1970];
[[NSUserDefaults sharedUserDefaults] setFloat:myDateTimeInterval forKey:@"myDateKey"];
// Get
NSTimeInterval myDateTimeInterval = [[NSUserDefaults sharedUserDefaults] floatForKey:@"myDateKey"];
NSDate *myDate = [NSDate dateWithTimeIntervalSince1970:myDateTimeInterval];
Pros and Cons
Option 1
This seems to be compact and logical. However, I've got worries about this going wrong because of Date Formatter bugs.
Option 2
This seems to be clumsy. I'm also unsure of the accuracy of it - in one test I did, when I retrieved the date back it was out by 48 seconds, despite the Apple Docs saying that NSTimeInterval has "subsecond accuracy".
Requirements
Whatever method I choose, it must be:
Precise to within a second.
Readable and reliable.
My Question
Is the inaccuracy with Option 2 because I'm doing something wrong?
Which out of these two options would you use?
Is there another option that I'm not aware of?
Thanks!