I have some code I use to sort calendar dates that looks like this:
#if !(TARGET_IPHONE_SIMULATOR)
NSString *formatString = [NSDateFormatter dateFormatFromTemplate:@"HH:mm dd MMM yyyy" options:0
locale:[NSLocale currentLocale]];
[fmt setDateFormat:formatString];
#else
[fmt setDateFormat:@"HH:mm dd MMM yyyy"];
#endif
If I run it in the simulator all is ok. If I run it on the device I get this sarcastic debug message.
2012-09-19 22:40:13.972 APPNAME [4923:907] * -[__NSCFCalendar
components:fromDate:]: date cannot be nilI mean really, what do you
think that operation is supposed to mean with a nil date?An exception has been avoided for now.
A few of these errors are going to be
reported with this complaint, then further violations will simply
silently do whatever random thing results from the nil. Here is the
backtrace where this occurred this time (some frames may be missing
due to compiler optimizations):
You have to laugh at it but I’m not sure what is wrong with my dateFormatFromTemplate: code. Any help would be appreciated.
Running Xcode V 4.5 btw
UPDATE:
Backtrace:
0 CoreFoundation 0x39ff0e55 + 84
1
APPNAME 0x00040be1
-[MeetingsViewController dateAtBeginningOfDayForDate:] + 140
So I guess things are going bad in my dateAtBeginningOfDayForDate method. Which looks like this:
/*
Break down a given NSDate to its bare components for sorting
*/
- (NSDate *)dateAtBeginningOfDayForDate:(NSDate *)inputDate
{
// Use the user's current calendar and time zone
NSCalendar *calendar = [NSCalendar currentCalendar];
NSTimeZone *timeZone = [NSTimeZone systemTimeZone];
[calendar setTimeZone:timeZone];
// Selectively convert the date components (year, month, day) of the input date
NSDateComponents *dateComps = [calendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:inputDate];
// Set the time components manually
[dateComps setHour:0];
[dateComps setMinute:0];
[dateComps setSecond:0];
// Convert back
NSDate *beginningOfDay = [calendar dateFromComponents:dateComps];
return beginningOfDay;
}
I use this method to break down a given NSDate to its’ core components so that I can sort them all more efficiently. However, my app has to be international which is the reason behind using NSLocale to formate the date output. From what it seems I will need to alter my dateAtBeginningOfDayForDate to work internationally as well.
Much thanks to nneonneo for helping me figure this out.
My issue is that my app has to work internationally so I needed dates to display with respect to the user location. I thought that I would just create a
NSDateFormatterto the users locale then pass in a string date like so:However, the problem is that I was localization the date format before I passed in the string date. In my case
NSDateFormatterlooked like this after localizing.Which I the format after localization could look many different ways after localizing.
So passing in a string with a format of
HH:mm dd MMM yyyywas causing it to returnnil.Which is obvious now -.-
So instead of localizing when I create the date, I create a the dates with a standard format of
HH:mm dd MMM yyyy. Then when displaying, I format theNSDateto the users respective locale.