当应用程序退出时,我正在将已转换为字符串的日期保存到plist.当viewDidLoad运行时,我试图读回字符串并将其转换回日期.该字符串保存到plist并正确读入但不会转换回日期.我使用的是NSDate date命令给出的标准格式.Time1始终为null.
输出如下所示:
time1string = 2010-07-07 13:47:12 -0500
time1 = (null)
Run Code Online (Sandbox Code Playgroud)
代码如下所示:
- (void)applicationWillTerminate:(NSNotification *)notification {
NSMutableArray *array = [[NSMutableArray alloc] init];
[array addObject: [NSString stringWithFormat:@"%@", time1]];
[array writeToFile:[self dataFilePath] atomically:YES];
[array release];
NSLog(@"APP Terminating, Persisting Data");
Run Code Online (Sandbox Code Playgroud)
}
- (void)viewDidLoad {
NSLog(@"View did load");
NSString *filePath = [self dataFilePath];
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
NSArray *array = [[NSArray alloc] initWithContentsOfFile:filePath];
NSString *time1string = [array objectAtIndex:1];
NSLog(@"time1string = %@",time1string);
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyy-MM-dd hh:mm:ss Z"];
time1 = [dateFormat dateFromString:time1string];
NSLog(@"time1 = %@",time1);
[dateFormat release];
[array release];
}
UIApplication *app = [UIApplication sharedApplication];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(applicationWillTerminate:)
name:UIApplicationWillTerminateNotification
object:app];
[super viewDidLoad];
Run Code Online (Sandbox Code Playgroud)
}
看起来它没有解析,因为你使用了错误的日期格式.请尝试使用@"yyyy-MM-dd HH:mm:ss Z".的hh格式需要在1-12的范围小时,而HH使用0-23.
有关Locale Data Markup Language可在此处使用的所有模式的完整参考,请参阅(通过数据格式化程序参考).
| 归档时间: |
|
| 查看次数: |
3426 次 |
| 最近记录: |