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; }
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.
The error does not happen in the code you posted. It means somewhere you have something that is supposed to be an NSDate
and instead it's nil
.
As for the humor in the error message, I have never seen that message, but +1 to Apple. Some engineer in the Cocoa division is as funny as the good old engineers Apple used to have a lot of time ago.
Check out the kind of diagnostics Apple's MPW C compiler used to spit out http://www.netfunny.com/rhf/jokes/91q3/cerrors.html
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 NSDateFormatter
to the users locale then pass in a string date like so:
NSString *formatString = [NSDateFormatter dateFormatFromTemplate:@"HH:mm dd MMM yyyy" options:0 locale:[NSLocale currentLocale]]; date = [fmt dateFromString:[NSString stringWithFormat:@"%@ %@", obj.meetingTime, obj.meetingDate]];
However, the problem is that I was localization the date format before I passed in the string date. In my case NSDateFormatter
looked like this after localizing.
MMM dd, yyyy, HH:mm
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 yyyy
was causing it to return nil
.
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 the NSDate
to the users respective locale.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With