2015-10-27 53 views
0

我試圖在NSDictionary中添加一個nil日期的崩潰。NSDateFormatter的dateFromString從stringFromDate返回null和日期

我的代碼如下所示:

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 

另外,我99%肯定這發生在巴西,在哪裏Daylight Saving Time occured 10月18日。

+1

嘗試打印您的日期字符串,並驗證它的格式是否與您使用的格式相同,即y-M-d – rptwsthi

+0

我無法通過「Crittercism」獲得崩潰報告。我自己無法重現。但從外表看,沒有外部因素,應該沒問題。 –

+1

解決方法是,在添加到字典之前,您可以檢查日期不爲空。 – Vijay

回答

1

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

這確實是巴西午夜時分發生的夏令時問題。因此,10月17日至18日,沒有午夜。因爲我的日期格式設置爲y-M-d,所以dateFromString:正試圖在當地午夜返回一個不存在的日期,因此它返回nil

重現步驟:

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

我還是要找出將返回我喜歡2015-10-18 01:00:00日期爲這個時區的解決方案。

+0

啊,Jon的答案是我忘了的一個經典...既然你可以檢測到由於'nil'返回或者使用['getObjectValue:forString:range:error:'在NSCocoaErrorDomain中返回錯誤] ://gist.github.com/DarkDust/5fbfafb3c9350b38666a)你可以通過追加時間並回退到包含時間的不同日期格式器來修改字符串。你可能也想試用[CFDateFormatter](https://developer.apple.com/library/mac/documentation/CoreFoundation/Reference/CFDateFormatterRef/):也許它有同樣的問題,也許不是。 – DarkDust

+0

@DarkDust,我可以做一些類似'dateComponents.hour = 8'的東西,但它會在100%時間內解決嗎? –

+0

我想你不能確定。我們可以看看今天的時區信息,並可能確定是的,它每次都會起作用。但是下一次[金正恩抽菸太多](https://en.wikipedia.org/wiki/Time_in_North_Korea)還是其他一些國家出現了一些愚蠢的事情呢?由於這是一個很少發生的問題的回退,所以您可能相當安全,採用這種解決方法。但是解決這個問題的最好方法是通過在第一個循環中存儲字符串和日期來完全消除對'dateFromString:'的調用。 – DarkDust

相關問題