I am attempting to store and retrieve a startTime and endTime NSDate with NSUserDefaults.
Storing them seems to be a non issue:
self.convertedStringToDate = convertDate
NSLog(@"This should read 16:00: %@", self.convertedStringToDate);
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:convertDate forKey:@"startDate"];
[userDefaults synchronize];
I then retrieve it just to make sure it is storing and retrieving correctly:
NSDate* temporaryDate = (NSDate*)[userDefaults objectForKey:@"startDate"];
NSLog(@"Now User Defaults, should read 16:00: %@", temporaryDate);
Looking at my NSLog, everything is fine:
2012-07-19 14:38:26.002 app[1973:707] This should read 16:00: 2012-07-19 16:00:00 +0000
2012-07-19 14:38:26.022 app[1973:707] Now User Defaults, should read 16:00: 2012-07-19 16:00:00 +0000
The problem now is that when I retreive the date again, in another method, the date comes up null:
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSDate *tmpDate = (NSDate*)[userDefaults objectForKey:@"startTime"];
NSLog(@"Retreiving date, should be 16:00: %@",tmpDate);
as shown in this log:
2012-07-19 14:38:26.107 app[1973:707] Retreiving date, should be 16:00: (null)
Does anyone have any ideas? I also tried not retrieving the date in the check, but the date continues to return null (and also this doesn’t make sense, NSUSerDefaults is a database which these keys and objects are stored). I also tried storing retained properties of the objects, and still to no avail. What am I missing?
EDIT: It is important to read your own code for stupid errors.
The Code is correct but…
tmpDate = [userDefaults objectForKey:@”startTime“];
should be
tmpDate = [userDefaults objectForKey:@”startDate“];