2014-12-05 41 views
-1

我從服務器獲取數據等當 - [__ NSArrayM insertObject:atIndex:]:對象不能是零」

listremainders (
     { 
     Remainter = "2014-12-28T19:42:39"; 
     RemainterDate = "2014-12-04 10:24:57"; 
     RemainterID = 16; 
     RemainterNotes = hjg; 
    } 

在這我正在檢索‘Remainter值’。 在控制檯還它顯示像 選項dateObj( 「2014-12-28T19:42:39」 )

我寫了這樣的代碼來獲取節數日期從API.Here是我的代碼...

datesArrayOfString = [[NSMutableArray alloc] init]; 

    for (NSString *dic in (NSMutableArray *)dateObj) { 

     NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
     [dateFormatter setDateFormat:@"yyyy-MM-ddTHH:mm:ss"];//date format cin 
     NSDate *sDate = [dateFormatter dateFromString:dic]; 
     // NSDateFormatter *dateFormatterTwo = [[NSDateFormatter alloc] init]; 
     [dateFormatter setDateFormat:@"EEEE dd MMM"]; 
     NSString *dateSTring=[dateFormatter stringFromDate:sDate]; 
     [datesArrayOfString addObject:dateSTring]; 
    } 

    NSLog(@"array of strings are %@", datesArrayOfString); 

該應用程序在發生循環時發生崩潰。它正在像...一樣。

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil' 
*** First throw call stack: 
(
    0 CoreFoundation      0x0227bdf6 __exceptionPreprocess + 182 
    1 libobjc.A.dylib      0x01f05a97 objc_exception_throw + 44 
    2 CoreFoundation      0x021331e1 -[__NSArrayM insertObject:atIndex:] + 881 
    3 CoreFoundation      0x02132e41 -[__NSArrayM addObject:] + 65 
    4 TimeTableApp      0x00184416 -[OneToOneViewController CallingListRemaindersServices] + 1926 
    5 TimeTableApp      0x0017da13 -[OneToOneViewController viewDidLoad] + 323 
    6 UIKit        0x00a46d54 -[UIViewController loadViewIfRequired] + 771 
    7 UIKit        0x00a47045 -[UIViewController view] + 35 
    8 UIKit        0x00a5ab09 -[UIViewController _setPresentationController:] + 103 
    9 UIKit        0x00a535c9 -[UIViewController _presentViewController:presentationController:animationController:interactionController:completion:] + 848 
    10 UIKit        0x00a54368 -[UIViewController _presentViewController:withAnimationController:completion:] + 1741 
    11 UIKit        0x00a57032 __62-[UIViewController presentViewController:animated:completion:]_block_invoke + 345 
    12 UIKit        0x00a56e84 -[UIViewController presentViewController:animated:completion:] + 224 
    13 UIKit        0x00fc3011 -[UIPopoverController _presentShimmedPopoverFromRect:inView:permittedArrowDirections:animated:] + 217 
    14 UIKit        0x00fc3211 -[UIPopoverController presentPopoverFromRect:inView:permittedArrowDirections:animated:] + 355 
    15 TimeTableApp      0x001067de -[TimeTableViewController collectionView:didSelectItemAtIndexPath:] + 61534 
    16 UIKit        0x01002dc8 -[UICollectionView _selectItemAtIndexPath:animated:scrollPosition:notifyDelegate:] + 591 
    17 UIKit        0x01020612 -[UICollectionView _userSelectItemAtIndexPath:] + 191 
    18 UIKit        0x01020806 -[UICollectionView touchesEnded:withEvent:] + 492 
    19 libobjc.A.dylib      0x01f1b7cd -[NSObject performSelector:withObject:withObject:] + 84 
    20 UIKit        0x00aa4b44 forwardTouchMethod + 270 
    21 UIKit        0x00aa4bb4 -[UIResponder touchesEnded:withEvent:] + 31 
    22 libobjc.A.dylib      0x01f1b7cd -[NSObject performSelector:withObject:withObject:] + 84 
    23 UIKit        0x00aa4b44 forwardTouchMethod + 270 
    24 UIKit        0x00aa4bb4 -[UIResponder touchesEnded:withEvent:] + 31 
    25 libobjc.A.dylib      0x01f1b7cd -[NSObject performSelector:withObject:withObject:] + 84 
    26 UIKit        0x00aa4b44 forwardTouchMethod + 270 
    27 UIKit        0x00aa4bb4 -[UIResponder touchesEnded:withEvent:] + 31 
    28 libobjc.A.dylib      0x01f1b7cd -[NSObject performSelector:withObject:withObject:] + 84 
    29 UIKit        0x00aa4b44 forwardTouchMethod + 270 
    30 UIKit        0x00aa4bb4 -[UIResponder touchesEnded:withEvent:] + 31 
    31 UIKit        0x00d28257 _UIGestureRecognizerUpdate + 13225 
    32 UIKit        0x0094171b -[UIWindow _sendGesturesForEvent:] + 1356 
    33 UIKit        0x0094257f -[UIWindow sendEvent:] + 769 
    34 UIKit        0x00907aa9 -[UIApplication sendEvent:] + 242 
    35 UIKit        0x009178de _UIApplicationHandleEventFromQueueEvent + 20690 
    36 UIKit        0x008ec079 _UIApplicationHandleEventQueue + 2206 
    37 CoreFoundation      0x0219f7bf __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15 
    38 CoreFoundation      0x021952cd __CFRunLoopDoSources0 + 253 
    39 CoreFoundation      0x02194828 __CFRunLoopRun + 952 
    40 CoreFoundation      0x021941ab CFRunLoopRunSpecific + 443 
    41 CoreFoundation      0x02193fdb CFRunLoopRunInMode + 123 
    42 GraphicsServices     0x04c3924f GSEventRunModal + 192 
    43 GraphicsServices     0x04c3908c GSEventRun + 104 
    44 UIKit        0x008efe16 UIApplicationMain + 1526 
    45 TimeTableApp      0x0018f40d main + 141 
    46 libdyld.dylib      0x0314aac9 start + 1 
) 
libc++abi.dylib: terminating with uncaught exception of type NSException 

我不知道爲什麼sDate越來越DIC「nil'.But數據在未來的',但它不是在存儲‘SDATE’。

我做了什麼錯誤? 任何人都可以幫助解決這個問題。我是Objective-C的新手。 在此先感謝。

+1

嘗試'YYYY-MM-dd'T'HH:MM:ss' – 2014-12-05 12:11:19

+1

您應該創建並設置兩個日期的格式循環外的格式化程序。你應該總是檢查'dateFromString'的結果爲零。而且你不能在NSArray或NSDictionary中插入一個nil。而'T'需要用日期格式字符串單引號。 – 2014-12-05 13:17:02

回答

0

你的API發送給您磁盤陣列和字典裏面,所以你的循環必須像這些:

for (NSDictionary *dictionary in NSArray) 
{ 
    for (NSString *key in dictionary) 
    { 
     id value = [dictionary objectForKey:key]; 
     // do stuff 
    } 
} 
+0

非常感謝您的回覆 – iworld 2014-12-05 13:20:45

1

小心-dateFromString方法,注意:

返回值一個日期使用 接收器的當前設置解釋的字符串表示。如果dateFromString:無法解析 字符串,則返回nil。

Reference Apple Documentation here

如果您不能解析的任何條目,你應該檢查在DateFormat屬性,see this所示模式。無論如何,你應該總是檢查輸入日期格式是否與你輸入的字符串匹配,因爲如果格式不匹配,你會得到零。

爲了避免這種情況,檢查SDATE不是零:

NSDate *sDate = [dateFormatter dateFromString:dic]; 
if (sDate != nil) 
    ... 
相關問題