Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What kind of sarcastic error is this iOS?

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.

like image 487
random Avatar asked Sep 20 '12 03:09

random


2 Answers

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

like image 99
Analog File Avatar answered Oct 12 '22 02:10

Analog File


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.

like image 26
random Avatar answered Oct 12 '22 04:10

random