我从服务器获取数据,如
listremainders (
{
Remainter = "2014-12-28T19:42:39";
RemainterDate = "2014-12-04 10:24:57";
RemainterID = 16;
RemainterNotes = hjg;
}
因为我正在检索“剩余价值”。 在控制台中它也显示为 dateObj( " 2014-12-28T19:42:39" )
我写了这样的代码来从API中检索no.of日期。这是我的代码......
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
得到'nil'。但数据是以'dic'形式出现的,但它并没有存储在'sDate'中。
我做错了什么? 任何人都可以帮助解决这个问题。我是Objective-c的新手。 在此先感谢。
答案 0 :(得分:1)
注意-dateFromString方法,注意:
返回值使用。解释的字符串的日期表示 接收器的当前设置。如果dateFromString:无法解析 string,返回nil。
Reference Apple Documentation here
如果您无法解析任何条目,则应检查dateFormat属性see this中指示的模式。无论如何,您应该始终检查输入日期格式是否与您的输入字符串匹配,因为如果格式不匹配,您将获得nil。
为避免这种情况,请检查sDate是否为零:
NSDate *sDate = [dateFormatter dateFromString:dic];
if (sDate != nil)
...
答案 1 :(得分:0)
你的api向你发送带有Dictionary的数组,所以你的循环必须如下所示:
for (NSDictionary *dictionary in NSArray)
{
for (NSString *key in dictionary)
{
id value = [dictionary objectForKey:key];
// do stuff
}
}