0

当我在我的设备上运行它(但不是在模拟器上)时,我遇到了最奇怪的错误:

我从 JSON 对象中获取日期,如下所示:

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];

如您所见,我使用格式化程序将字符串转换为日期,这很有效。然后我创建 NSDateComponents,然后用它来做elapsedTimeUnits.hour等等.day来计算从dateXNSDate date(现在)的时间量。

它有效!或者我认为,在模拟器上测试所有内容时。

我刚刚收到这个疯狂的错误消息,说 dateX 为零,但只有当我在我的设备上运行它时。所以每次我在模拟器中运行它时,它都会显示从日期开始的正确时间。当我在设备上运行它时,它会显示一个特定的日期,即 4 周、1 天和 23 小时前。我已经 NSLogged 了[info objectForKey:@"created_at"],并且知道它得到了正确的日期作为开始的输入。显然,该应用程序无法在设备上进行从字符串到日期的转换,而是在模拟器上进行。从这里开始的一切都是我得到的输出:

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

)
4

2 回答 2

2

检查设备上的设置并确保 12/24 小时模式与设备区域设置一致。(美国 12 小时,世界其他大部分地区 24 小时。)

美元与甜甜圈发生冲突,使您陷入iOS“费用”。

看到这个线程

于 2012-08-31T00:01:19.050 回答
0

我为我解决了这个问题。我希望这对任何人都有帮助。我的问题是注释的代码。我用这个注释代码下面的新书面代码解决了这个问题。代码如下:

//    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);
于 2013-07-18T12:50:48.913 回答