我正在尝试使用 NSUserDefaults 存储和检索 astartTime
和endTime
NSDate。存储它们似乎不是问题:
self.convertedStringToDate = convertDate
NSLog(@"This should read 16:00: %@", self.convertedStringToDate);
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:convertDate forKey:@"startDate"];
[userDefaults synchronize];
然后我检索它只是为了确保它正确存储和检索:
NSDate* temporaryDate = (NSDate*)[userDefaults objectForKey:@"startDate"];
NSLog(@"Now User Defaults, should read 16:00: %@", temporaryDate);
查看我的 NSLog,一切都很好:
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
现在的问题是,当我再次检索日期时,在另一种方法中,日期出现为空:
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSDate *tmpDate = (NSDate*)[userDefaults objectForKey:@"startTime"];
NSLog(@"Retreiving date, should be 16:00: %@",tmpDate);
如本日志所示:
2012-07-19 14:38:26.107 app[1973:707] Retreiving date, should be 16:00: (null)
有没有人有任何想法?我也尝试不检索支票中的日期,但日期继续返回 null(这也没有意义,NSUSerDefaults 是存储这些键和对象的数据库)。我还尝试存储retained
对象的属性,但仍然无济于事。我错过了什么?
编辑:阅读您自己的代码以了解愚蠢的错误很重要。