0

I'm getting the weirdest error when I run this on my device (but not on simulator):

I am getting a date from a JSON object, like this:

NSDateFormatter *df = [[NSDateFormatter alloc]init];
[df setDateFormat:@"EEE MMM d HH:mm:ss Z yyyy"];
NSDate *dateX = [df dateFromString: [info objectForKey:@"created_at"]];


NSUInteger desiredComponents = NSYearCalendarUnit | NSMonthCalendarUnit  | 
                               NSWeekCalendarUnit | NSHourCalendarUnit   | 
                               NSDayCalendarUnit  | NSMinuteCalendarUnit | 
                               NSSecondCalendarUnit;

NSDateComponents *elapsedTimeUnits = [[NSCalendar currentCalendar] 
                                      components:desiredComponents
                                      fromDate:dateX
                                      toDate:[NSDate date]
                                      options:0];

As you can see, I use the formatter to convert the string into a date, which works. Then I create the NSDateComponents, and later use that to do elapsedTimeUnits.hour and .day etc. to calculate the amount of time from dateX to NSDate date(NOW).

And it works! Or so I thougt, while testing everything on the simulator.

I just got this insane errormessage saying dateX was nil, but only when I ran it on my device. So every time I run this in the simulator, it shows the correct amount of time from the date. When I run it on the device, it shows one specific date, which is 4 weeks, 1 day and 23 hours ago. I have NSLogged out the [info objectForKey:@"created_at"], and knows it gets the correct date as input to begin with. Apparently, the app can't do the converting from string to date on the device, but on the simulator. Everything from here on out is the output I get:

2012-08-31 00:26:19.773 App[1676:907] *** -[__NSCFCalendar 
components:fromDate:toDate:options:]: fromDate cannot be nil

I mean really, what do you think that operation is supposed to mean with a nil fromDate?

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):

(

0   CoreFoundation                      0x36a5e8bf <redacted> + 86

1   App                                0x00082d67 -[NewsViewController tableView:cellForRowAtIndexPath:] + 478

2   UIKit                               0x384d1b81 <redacted> + 412

3   UIKit                               0x384b693b <redacted> + 1310

4   UIKit                               0x384cde17 <redacted> + 206

5   UIKit                               0x38489f2b <redacted> + 258

6   QuartzCore                          0x357d7e9b <redacted> + 214

7   QuartzCore                          0x357d7a39 <redacted> + 460

8   QuartzCore                          0x357d8975 <redacted> + 16

9   QuartzCore                          0x357d8353 <redacted> + 238

10  QuartzCore                          0x357d8161 <redacted> + 316

11  UIKit                               0x3849000b <redacted> + 126

12  CoreFoundation                      0x36a8caed <redacted> + 20

13  CoreFoundation                      0x36a8ade1 <redacted> + 276

14  CoreFoundation                      0x36a8b137 <redacted> + 742

15  CoreFoundation                      0x369fe39d CFRunLoopRunSpecific + 356

16  CoreFoundation                      0x369fe229 CFRunLoopRunInMode + 104

17  GraphicsServices                    0x353b331b GSEventRunModal + 74

18  UIKit                               0x384da8f9 UIApplicationMain + 1120

19  App                                0x0006107f main + 86

20  App                                0x0005f450 start + 40

)
Sti
  • 8,275
  • 9
  • 62
  • 124
  • Can you do `NSLog(@"%@", [info objectForKey:@"created_at"])` and run it on your device to verify that your created_at string has a value and the format is the same format you get in the simulator? – jonkroll Aug 30 '12 at 23:04
  • @jonkroll - I have. And it does match. That was literally one of the last things I wrote. – Sti Aug 30 '12 at 23:07
  • Oops, sorry, missed that in your description. – jonkroll Aug 30 '12 at 23:09
  • How do the locale / calendar / time zone settings differ between your computer and your phone, if at all? That is, does `+[NSCalendar currentCalendar]` return a calendar with the same `-calendarIdentifier`? – Carl Veazey Aug 30 '12 at 23:45
  • @CarlVeazey - You are right. In one way. Both return gregorian identifier, but I noticed that my simulator ran with United Stated as region format in "Settings". When I sat it to Norway, as my own phone, it showed the same wrong timestamps as my device, but strangely enough I didn't get the output.. Nearly missed the error. They both show different locale, but that seems deprecated anyway, so I'm not sure what to do. The string is always in 24h gmt, so I have to find a way to format this on all devices, even if it is in the states. – Sti Aug 31 '12 at 00:20

2 Answers2

2

Check your Settings on the device and make sure that 12/24 hour mode is consistent with the device locale. (12 hours in the US, 24 in most other parts of the world.)

Dollars to donuts they're in conflict, bumping you into a iOS "feechure".

See this thread.

Community
  • 1
  • 1
Hot Licks
  • 47,103
  • 17
  • 93
  • 151
  • Thank you! That was it. My simulator was set to United States on region format, and my device to Norway. 12h vs 24h. Still, one heck of an error-log. Had some attitude. "I mean really?". So yeah.. now neither works. I'll check out that thread. Thanks- – Sti Aug 31 '12 at 00:23
  • 1
    If anyone else wonders, I added this line to fix it: `[df setLocale:([[NSLocale alloc] initWithLocaleIdentifier:@"en_UK"])];` (Before `NSDate *dateX`. – Sti Aug 31 '12 at 02:01
  • @StianFlatby -- You want to double-check that the en_UK locale will work with 12/24 set either way. "en_US_POSIX" is the only locale that I know works for sure, either way. – Hot Licks Aug 31 '12 at 02:20
0

I fixed this issue for me. I hope this can help to anyone. My problem was with the code which is commented. I solved the problem with new written code which are below this commented code. Code is given as:

//    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    //    df.dateStyle = NSDateFormatterMediumStyle;
    //
    //    NSCalendar *calendar = [NSCalendar currentCalendar];
    //    int year   =    [[calendar components:NSYearCalendarUnit  fromDate:[datePickerView date]] year];
    //    int month  =    [[calendar components:NSMonthCalendarUnit fromDate:[datePickerView date]] month];
    //    int day    =    [[calendar components:NSDayCalendarUnit   fromDate:[datePickerView date]] day];
    //    

    NSDateComponents *components = [[NSCalendar currentCalendar] components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:[datePickerView date]];
    NSInteger day = [components day];
    NSInteger month = [components month];
    NSInteger year = [components year];

    date = [NSString stringWithFormat:@"%d-%d-%d",year,month, day];
    NSLog(@"Updated Date: %@",date);
iDevAmit
  • 1,550
  • 2
  • 21
  • 33