-1

I have an NSDate that I am saving into NSUserDefaults.

When my app runs this code for a 2nd pass, I am expecting an NSDate to be retrieved from NSUserDefaults but, it is always nil. Not sure why.

NSDate *mostRecentMentionDate = [dateFormatter dateFromString:mostRecentMentionMessageTimestamp];
NSDate *savedMentionDate = [[NSUserDefaults standardUserDefaults] objectForKey:@"mostRecentMentionDate"];

if (savedMentionDate == nil || [savedMentionDate isEqual:[NSNull null]]) {
    //There is no existing mention, so save the most recent one
    [[NSUserDefaults standardUserDefaults]setObject:mostRecentMentionDate forKey:@"mostRecentMentionDate"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}
Doug Smith
  • 29,668
  • 57
  • 204
  • 388
Sheehan Alam
  • 60,111
  • 124
  • 355
  • 556
  • 1
    You do not need to convert the `NSDate` to/from a string, `NSUserDefaults` is perfectly capable of storing `NSDate` objects, and you're just making things complicated and slow by doing the conversion yourself. See http://stackoverflow.com/questions/2013850/whats-the-optimum-way-of-storing-an-nsdate-in-nsuserdefaults – Abhi Beckert Nov 10 '11 at 08:22
  • My date originally comes in as a String, thats why I have to turn it into an NSDate via dateFromString: – Sheehan Alam Nov 10 '11 at 16:45
  • `if (savedMentionDate == nil || [savedMentionDate isEqual:[NSNull null]])` - This check is entirely redundant. Just check if it's equal to nil and call it a day. – sudo rm -rf Nov 28 '11 at 17:49

3 Answers3

1

if i use this code it work fine i think problem with your dateformater

NSDate *mostRecentMentionDate = [NSDate date];
NSDate *savedMentionDate = [[NSUserDefaults standardUserDefaults] objectForKey:@"mostRecentMentionDate"];

if (savedMentionDate == nil || [savedMentionDate isEqual:[NSNull null]]) {
  //There is no existing mention, so save the most recent one
  [[NSUserDefaults standardUserDefaults]setObject:mostRecentMentionDate forKey:@"mostRecentMentionDate"];
  [[NSUserDefaults standardUserDefaults] synchronize];
}
Abhi Beckert
  • 32,787
  • 12
  • 83
  • 110
Rahul Juyal
  • 2,124
  • 1
  • 16
  • 33
1

Are you sure you're getting a valid object for mostRecentMentionDate? Put it into NSLog right before fetching savedMentionDate from NSUserDefaults, you might be having problems with date format.

Eugene
  • 10,006
  • 4
  • 37
  • 55
0

NSDate confirm to NSCoding protocol. above code should work.

Try this -

/

/ Create and store it
NSDate * date = [NSDate date];
NSData * dateData = [NSKeyedArchiver archivedDataWithRootObject:date];
[dateData writeToFile:@"/Some/path/to/file.dat" atomically:NO];

// Now bring it back
NSData * restoredDateData = [NSData dataWithContentsOfFile:@"/Some/path/to/file.dat"];
NSDate * restoredDate = [NSKeyedUnarchiver unarchiveObjectWithData:restoredDateData];

Or you Can get timeIntervalSinceReferenceDate value as a NSNumber (double) and save it to NSUserDefault to keep track of previous date.

iOSPawan
  • 2,884
  • 2
  • 25
  • 50