简体   繁体   中英

Unable to convert string to NSDate only iOS 13.3

I have a string that has date and time. When trying to convert the string to NSDate, it is getting nil and App Crashes. It is working fine with other iOS versions 13, 13.1 and 13.2. It is only crashing in iOS 13.3.

Code Snippet:

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

In the above-mentioned code when I debug startDate is getting nil in iOS 13.3 but it is working fine with lower iOS versions.

Crash Report:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM