title: What's the optimum way of storing an NSDate in NSUserDefaults?
body: <p>There's two ways of storing an NSDate in NSUserDefaults that I've come across.</p>
  
  <h2>Option 1 - setObject:forKey:</h2>
  
  <pre><code>// Set
  NSDate *myDate = [NSDate date];
  [[NSUserDefaults standardUserDefaults] setObject:myDate forKey:@"myDateKey"];
  
  // Get
  NSDate *myDate = (NSDate *)[[NSUserDefaults standardUserDefaults] objectForKey:@"myDateKey"];
  </code></pre>
  
  <h2>Option 2 - timeIntervalSince1970</h2>
  
  <pre><code>// Set
  NSDate *myDate = [NSDate date];
  NSTimeInterval myDateTimeInterval = [myDate timeIntervalSince1970];
  [[NSUserDefaults standardUserDefaults] setFloat:myDateTimeInterval forKey:@"myDateKey"];
  
  // Get
  NSTimeInterval myDateTimeInterval = [[NSUserDefaults standardUserDefaults] floatForKey:@"myDateKey"];
  NSDate *myDate = [NSDate dateWithTimeIntervalSince1970:myDateTimeInterval];
  </code></pre>
  
  <h2>Pros and Cons</h2>
  
  <p><strong>Option 1</strong></p>
  
  <p>This seems to be compact and logical. However, I've got worries about this going wrong because of <a href="http://osdir.com/ml/cocoa-dev/2009-04/msg01935.html" rel="noreferrer">Date Formatter bugs</a>. </p>
  
  <p><strong>Option 2</strong></p>
  
  <p>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".</p>
  
  <h2>Requirements</h2>
  
  <p>Whatever method I choose, it must be:</p>
  
  <ol>
  <li><p>Precise to within a second.</p></li>
  <li><p>Readable and reliable.</p></li>
  </ol>
  
  <h2>My Question</h2>
  
  <p>Is the inaccuracy with Option 2 because I'm doing something wrong?</p>
  
  <p>Which out of these two options would you use?</p>
  
  <p>Is there another option that I'm not aware of?</p>
  
  <p>Thanks!</p>