I am saving a date that has been converted to a string to a plist when the app exits. When the viewDidLoad is run I am trying to read the string back in and convert it back to a date. The string saves to the plist and reads in correctly but will not convert back to a date. I am using the standard format given by the NSDate date command. Time1 is always null.
The output looks like this:
time1string = 2010-07-07 13:47:12 -0500
time1 = (null)
The code looks like this:
- (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");
}
-(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];
}