Having a very odd problem. After processing of a file I read a txt which has three lines. Version, From Datetime, ToDatetime. I read these lines and add an object to NSCoreData. Everything works fine when running in a simulator but as soon as I deploy to my iPhone the NSDates come back as null.
Here is the console output when running from iPhone
2012-01-05 23:58:25.478 FlightPath[10257:707] 7/28/2011 12:00:00 AM
2012-01-05 23:58:27.911 FlightPath[10257:707] (null)
2012-01-05 23:58:29.718 FlightPath[10257:707] (null)
Here is the console output when running form iPhone Sim
2012-01-06 00:07:07.900 FlightPath[1755:11903] 7/28/2011 12:00:00 AM
2012-01-06 00:07:08.899 FlightPath[1755:11903] 7/28/2011 12:00:00 AM
2012-01-06 00:07:11.686 FlightPath[1755:11903] 7/28/2011 12:00:00 AM
Here is the code…I added in tDate for testing purposes. I wanted to make sure i didn’t need to add __block to the NSManagedObject.
NSString *mapInfoPath = [[mapData objectAtIndex:1] stringByAppendingPathComponent: @"Tiles/info.txt"];
NSString *mapInfo = [[NSString alloc] initWithContentsOfFile:mapInfoPath encoding:NSUTF8StringEncoding error:nil];
__block NSDate *tDate = nil;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"M/d/yyyy h:mm:ss a"];
__block int lineCount = 0;
[mapInfo enumerateLinesUsingBlock:^(NSString *line, BOOL *stop) {
switch (lineCount) {
case 1:
map.editionNumber = [NSNumber numberWithInt:[line intValue]];
break;
case 2:
tDate = [dateFormatter dateFromString:line];
map.fromDate = [dateFormatter dateFromString:line];
NSLog(@"%@", line);
NSLog(@"%@", [dateFormatter stringFromDate:map.fromDate]);
NSLog(@"%@", [dateFormatter stringFromDate:tDate]);
break;
case 3:
map.toDate = [dateFormatter dateFromString:line];
break;
default:
break;
}
lineCount += 1;
}];
map.processed = [NSNumber numberWithBool:YES];
Any help would be great….stuck and can’t figure out why it works on sim and not iPhone.
Thanks!
Appears this is a bug when phone is in 24 hour mode. The direct answer was here: http://multinc.com/2009/09/27/iphone-sdk-time-bug-for-international-users/ which I found off of this post: Why my NSDate Formatter can't convert correctly and returns null on device?
Since I always know I’ll be parsing a 12hr date time I just used setLocale and that did the trick.