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 nil I 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; }