当我在我的设备上运行它(但不是在模拟器上)时,我遇到了最奇怪的错误:
我从 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
来计算从dateX
到NSDate 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
)