gpt4 book ai didi

ios - 无法将字符串转换为仅限 iOS 13.3 的 NSDate

转载 作者:行者123 更新时间:2023-11-28 17:48:34 26 4
gpt4 key购买 nike

我有一个包含日期和时间的字符串。尝试将字符串转换为 NSDate 时,它​​变为 nil 并且应用程序崩溃。它适用于其他 iOS 版本 13、13.1 和 13.2。它仅在 iOS 13.3 中崩溃。

代码片段:

NSString *startDateString = @"01-04-2020 8:00pm";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"MM-dd-yyyy h:mma";
NSDate *startDate = [dateFormatter dateFromString:startDateString];

在上面提到的代码中,当我调试 startDate 在 iOS 13.3 中为 nil 但它在较低的 iOS 版本中工作正常。

崩溃报告:

2020-01-06 10:04:28.312021+0530 LivingTree[1295:196522] *** Assertion failure in -[_UIDatePickerView _setDate:animated:forced:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKitCore/UIKit-3901.4.2/_UIDatePickerView.m:306
2020-01-06 10:04:28.313254+0530 LivingTree[1295:196522] CRASH: Invalid parameter not satisfying: date
2020-01-06 10:04:28.327693+0530 LivingTree[1295:196522] Stack Trace: (
0 CoreFoundation 0x0000000198bec980 93BD750D-2ECE-3E38-8405-9E46EFE482C6 + 1255808
1 libobjc.A.dylib 0x0000000198905028 objc_exception_throw + 60
2 CoreFoundation 0x0000000198ae94fc 93BD750D-2ECE-3E38-8405-9E46EFE482C6 + 193788
3 Foundation 0x0000000198f2a700 80C31587-F538-3437-966F-300F6632EB95 + 571136
4 UIKitCore 0x000000019cc5e634 AA897CA9-8D15-3DD7-BB4F-8D90F9A28571 + 10253876
5 UIKitCore 0x000000019cc5e310 AA897CA9-8D15-3DD7-BB4F-8D90F9A28571 + 10253072
6 UIKitCore 0x000000019cc4dd3c AA897CA9-8D15-3DD7-BB4F-8D90F9A28571 + 10186044
7 LivingTree 0x000000010018e930 -[CreateEventViewController setEditEventData] + 13568
8 LivingTree 0x000000010018b41c __40-[CreateEventViewController viewDidLoad]_block_invoke + 3344
9 libdispatch.dylib 0x0000000101ae1e1c _dispatch_call_block_and_release + 32
10 libdispatch.dylib 0x0000000101ae327c _dispatch_client_callout + 20
11 libdispatch.dylib 0x0000000101af132c _dispatch_main_queue_callback_4CF + 1376
12 CoreFoundation 0x0000000198b67cc8 93BD750D-2ECE-3E38-8405-9E46EFE482C6 + 711880
13 CoreFoundation 0x0000000198b62a24 93BD750D-2ECE-3E38-8405-9E46EFE482C6 + 690724
14 CoreFoundation 0x0000000198b61f40 CFRunLoopRunSpecific + 480
15 GraphicsServices 0x00000001a2ddf534 GSEventRunModal + 108
16 UIKitCore 0x000000019ccdaa60 UIApplicationMain + 1940
17 LivingTree 0x00000001001143f8 main + 124
18 libdyld.dylib 0x00000001989e0e18 9059087B-727B-3B45-B672-09E5B03E6020 + 3608
)
2020-01-06 10:04:28.328420+0530 LivingTree[1295:196522] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid parameter not satisfying: date'
*** First throw call stack:
(0x198bec96c 0x198905028 0x198ae94fc 0x198f2a700 0x19cc5e634 0x19cc5e310 0x19cc4dd3c 0x10018e930 0x10018b41c 0x101ae1e1c 0x101ae327c 0x101af132c 0x198b67cc8 0x198b62a24 0x198b61f40 0x1a2ddf534 0x19ccdaa60 0x1001143f8 0x1989e0e18)
libc++abi.dylib: terminating with uncaught exception of type NSException

最佳答案

 NSString *startDateString = @"01-04-2020 8:00pm";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"MM-dd-yyyy h:mma";
dateFormatter.locale = [[NSLocale alloc]initWithLocaleIdentifier:@"en_US_POSIX"];
dateFormatter.timeZone = [[NSTimeZone alloc]initWithName:@"UTC"];
NSDate *startDate = [dateFormatter dateFromString:startDateString];

关于ios - 无法将字符串转换为仅限 iOS 13.3 的 NSDate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59606674/

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