83

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.

random
  • 8,568
  • 12
  • 50
  • 85
  • 40
    OK, this did make me laugh. Someone at Apple has a cruel sense of humor... – nneonneo Sep 20 '12 at 03:56
  • 3
    It says there's a backtrace. What's in the backtrace? – nneonneo Sep 20 '12 at 03:56
  • @nneonneo It's about a mile long, not much in it really. The thing is that if I run the above code on the device with only `[fmt setDateFormat:@"HH:mm dd MMM yyyy"];` it runs fine. So I'm assuming that `dateFormatFromTemplate:` is wrong in some way. – random Sep 20 '12 at 04:09
  • 2
    Well, I was curious as to which one of those two lines crashed. What's the value of `formatString`? – nneonneo Sep 20 '12 at 04:15
  • @nneonneo **formatString = 'MMM dd, yyyy, HH:mm'** Maybe something to do with NSLocale stuff? I really don't know what I'm doing wrong. – random Sep 20 '12 at 04:20
  • 2
    Does it really crash at one of those two lines? That's where the backtrace would help... – nneonneo Sep 20 '12 at 04:21
  • 2
    I think it's pretty obvious from the code you posted and the error you got that `inputDate` must be `nil`. Look at the higher stack frames and see if you can figure out what might send the `nil` input date. – nneonneo Sep 20 '12 at 04:39
  • @nneonneo Right, the `dateFormatFromTemplate` is returning `nil`. Which I know, do you know why this would be returning `nil`? Do I have it setup wrong? If I set the dateFormatter directly it works fine, so I'm assuming that means `dateFormatFromTemplate` is wrong in some way. – random Sep 20 '12 at 04:46
  • 2
    First you say `formatString` is `MMM dd, yyyy, HH:mm`, then you say that `dateFormatFromTemplate`, whose result is set as `formatString`, is returning `nil`...I am rather confused. – nneonneo Sep 20 '12 at 04:50
  • @nneonneo I believe the problem is that I pass in `dateFormatFromTemplate:@"HH:mm dd MMM yyyy"` which then gets translated into `MMM dd, yyyy, HH:mm` based on user locale. So when I go to pass in a string date formatted as `HH:mm dd MMM yyyy` it breaks. So what I think I am going to have to do is sort the dates with out using a `NSDate` formatted to the user locale. However, when I go to actually **display** the dates, I format them to display in the users current locale. whew.. Thanks so much for your help! – random Sep 20 '12 at 04:55
  • Just got this error! Great! Nice too see an actual sense of humour, take note Microsoft. Then again, maybe Microsoft thinks blue screens are funny for developers – Adam Carter Mar 27 '13 at 21:12
  • I think this particular message is raised precisely to get your attention. The error occurs inside the bowels of iOS where an exception would not be nice and where bad data could cause bad things, so the code fakes things up as best it can. But it wants to draw your attention to the problem. – Hot Licks Jun 27 '13 at 17:53
  • I'd much rather have the exception thrown than some snarky comment that is utterly unhelpful. At least on an exception, I can set the debugger to break and I can see where it is happening, interrogate variables, etc. I get this occasionally. The date comes directly from a database record, and the database shows that valid dates are in all records. So it is not easy to figure out what is happening. -1 to Apple. (Now, if they had the snarky comment AND threw the exception, all would be good). – chadbag Jan 27 '14 at 21:54

2 Answers2

16

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

Analog File
  • 5,280
  • 20
  • 23
  • It's about a mile long, not much in it really. The thing is that if I run the above code on the device with only `[fmt setDateFormat:@"HH:mm dd MMM yyyy"];` it runs fine. So I'm assuming that `dateFormatFromTemplate:` is wrong in some way. – random Sep 20 '12 at 04:09
  • This comment is only related in terms of humor... I once had to modify the source to the SunOS SCSI disk driver (sd.c). There was a comment above some bit testing code that said /* snarf yummos from the tasty bits */ – JohnQ May 16 '13 at 14:14
6

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.

Community
  • 1
  • 1
random
  • 8,568
  • 12
  • 50
  • 85
  • You also need to be aware of the [locale "feature"](http://stackoverflow.com/questions/6613110/what-is-the-best-way-to-deal-with-the-nsdateformatter-locale-feature). – Hot Licks Jun 27 '13 at 17:50