gpt4 book ai didi

ios - 将字符串转换为日期到字符串并出现奇怪的错误! (仅在设备上运行时)

转载 作者:行者123 更新时间:2023-11-28 22:50:07 25 4
gpt4 key购买 nike

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

我正在从 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 日期(现在)。

而且有效!在模拟器上测试所有内容时,我是这么想的。

我刚刚收到这条疯狂的错误消息,说 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

)

最佳答案

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

Dollars to donuts 他们是冲突的,让你陷入 iOS“feechure”。

参见 this thread .

关于ios - 将字符串转换为日期到字符串并出现奇怪的错误! (仅在设备上运行时),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12206775/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com