NSDateFormatter的dateFromString从stringFromDate返回null并带有日期

时间:2015-10-27 08:22:13

标签: ios objective-c nsdateformatter

我尝试在nil中添加NSDictionary日期时遇到了崩溃。

我的代码如下所示:

625  NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
626  NSCalendar *currentCalendar = [NSCalendar autoupdatingCurrentCalendar];
627  NSDate *todayDate = [NSDate date];
628
629  NSDateComponents *dateComponents = [currentCalendar components:NSUIntegerMax fromDate:todayDate];
630
631  NSMutableArray *lastDates = [[NSMutableArray alloc] init];
632
633  dateFormatter.dateFormat = @"y-M-d";
634
635  for (NSInteger i = 0; i < 7; i++) {
636      NSDate *date = [currentCalendar dateFromComponents:dateComponents];
637
638      dateComponents.day--;
639
640      [lastDates addObject:[dateFormatter stringFromDate:date]];
641  }
642
643  NSString *query = @"SELECT date FROM table";
644  NSArray *completedDates = [ZeeSQLiteHelper readQueryFromDB:query];
645
646  NSMutableDictionary *completedLessons = [[NSMutableDictionary alloc] init];
647
648  for (NSString *date in lastDates) {
649      BOOL completed;
650
651      if ([completedDates containsObject:@{ @"date" : date }]) {
652          completed = YES;
653   } else {
654       completed = NO;
655   }
656
657   completedLessons[[dateFormatter dateFromString:date]] = @(YES); // crash
658  }

出于某种原因,有时,dateFromString:会返回nil并且崩溃。

崩溃报告:

0     CoreFoundation      0    x231d068b    __exceptionPreprocess + 124
1     libobjc.A.dylib     0    x3458ae17    objc_exception_throw + 36
2     CoreFoundation      0    x230ec8fb    -[__NSDictionaryM setObject:forKey:] + 840
3     MyApp               0    x00148df7    -[MyClass myMethod] (MyClass.m:657)
4     MyApp               0    x001406f3    -[MySecondViewController mySecondVCMethod] (MySecondViewController.m:250)
5     MyApp               0    x0013f8ed    -[MySecondViewController viewDidLoad] (MySecondViewController.m:92)
6     UIKit               0    x2729443d    -[UIViewController loadViewIfRequired] + 1106
7     UIKit               0    x27293fcd    -[UIViewController view] + 22
8     UIKit               0    x27abb559    -[_UIFullscreenPresentationController _setPresentedViewController:] + 70
9     UIKit               0    x275c9339    -[UIPresentationController initWithPresentedViewController:presentingViewController:] + 110
10    UIKit               0    x275ec473    -[UIViewController _presentViewController:withAnimationController:completion:] + 3536
11    UIKit               0    x275eeac5    __62-[UIViewController presentViewController:animated:completion:]_block_invoke + 334
12    UIKit               0    x275eed31    -[UIViewController _performCoordinatedPresentOrDismiss:animated:] + 414
13    UIKit               0    x27388701    -[UIViewController presentViewController:animated:completion:] + 142
14    MyApp               0    x00223429    -[MyFirstViewController myFirstVCSecondMethod] (MyFirstViewController.m:775)
15    MyApp               0    x00221edb    -[MyFirstViewController myFirstVCFirstMethod:] (MyFirstViewController.m:631)
16    UIKit               0    x272ca795    -[UIApplication sendAction:to:from:forEvent:] + 78
17    UIKit               0    x272ca725    -[UIControl sendAction:to:forEvent:] + 62
18    UIKit               0    x272b354b    -[UIControl _sendActionsForEvents:withEvent:] + 444
19    UIKit               0    x272ca085    -[UIControl touchesEnded:withEvent:] + 602
20    UIKit               0    x27286d33    _UIGestureRecognizerUpdate + 10768
21    UIKit               0    x272c36f9    -[UIWindow _sendGesturesForEvent:] + 902
22    UIKit               0    x272c2e43    -[UIWindow sendEvent:] + 620
23    UIKit               0    x272947e5    -[UIApplication sendEvent:] + 202
24    UIKit               0    x27292fdf    _UIApplicationHandleEventQueue + 4932
25    CoreFoundation      0    x23193c3f    __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 12
26    CoreFoundation      0    x2319382d    __CFRunLoopDoSources0 + 450
27    CoreFoundation      0    x23191b9b    __CFRunLoopRun + 792
28    CoreFoundation      0    x230e5249    CFRunLoopRunSpecific + 518
29    CoreFoundation      0    x230e5035    CFRunLoopRunInMode + 106
30    GraphicsServices    0    x2c1c8ad1    GSEventRunModal + 158
31    UIKit               0    x272fa899    UIApplicationMain + 142
32    MyApp               0    x00152f83    main (main.m:16)
33    libdyld.dylib       0    x34cd6873    start + 0

另外,我确信这种情况发生在10月18日的巴西,Daylight Saving Time occured

1 个答案:

答案 0 :(得分:1)

This answer和@DarkDust的comment指出了我正确的方向。

这确实是巴西午夜发生的夏令时问题。因此,10月17日至18日,没有午夜。因为我将日期格式设置为y-M-ddateFromString:试图在当地午夜返回一个不存在的日期,因此返回nil

重现的步骤:

dateFormatter.timeZone = [NSTimeZone timeZoneWithName:@"America/Sao_Paulo"];
NSLog([dateFormatter dateFromString:@"2015-10-18"]); // returns nil

我仍然需要找到一个解决方案,它会为此时区返回2015-10-18 01:00:00之类的日期。