将空对象添加到数组

326

我需要向使用来自第三方XML feed的数据填充的数组的索引0和索引1添加一个空对象。

这是我的parseXML方法,它可以工作。

  -(void) parseXML{

    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"APIKEYHERECANTSHOW YOU"]];

    NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

    NSString *xmlString = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];

    NSLog(@"The string : %@", xmlString);

    NSDictionary *xml = [NSDictionary dictionaryWithXMLString:xmlString];

    NSLog(@"The dict:%@", xml);

   NSMutableDictionary *PageItem = [xml objectForKey:@"TeamLeagueStanding"];

     NSLog(@"PageItem: %@", PageItem);

    NSMutableArray *items = [xml objectForKey:@"TeamLeagueStanding"];

    NSNull *nullValue = [NSNull null];

    [items insertObject:nullValue atIndex:0]; <- THIS MAKES MY APP CRASH

    NSLog(@"The array: %@", items);

    [self setTableData:items];
}

但是当我运行它时,控制台输出崩溃:

2014-02-03 21:24:09.063 Liga Zon Sagres Companion[9645:70b] -[NSNull objectForKey:]: unrecognized selector sent to instance 0x101a85b40
2014-02-03 21:24:09.066 Liga Zon Sagres Companion[9645:70b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSNull objectForKey:]: unrecognized selector sent to instance 0x101a85b40'
*** First throw call stack:
(
    0   CoreFoundation                      0x000000010192a795 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x000000010168d991 objc_exception_throw + 43
    2   CoreFoundation                      0x00000001019bbbad -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
    3   CoreFoundation                      0x000000010191c09d ___forwarding___ + 973
    4   CoreFoundation                      0x000000010191bc48 _CF_forwarding_prep_0 + 120
    5   Liga Zon Sagres Companion           0x000000010000ee20 -[StandingsTableViewController tableView:cellForRowAtIndexPath:] + 256
    6   UIKit                               0x00000001003bbb8a -[UITableView _createPreparedCellForGlobalRow:withIndexPath:] + 348
    7   UIKit                               0x00000001003a3836 -[UITableView _updateVisibleCellsNow:] + 2297
    8   UIKit                               0x00000001003b4381 -[UITableView layoutSubviews] + 207
    9   UIKit                               0x000000010034bb27 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 354
    10  QuartzCore                          0x0000000102081a22 -[CALayer layoutSublayers] + 151
    11  QuartzCore                          0x0000000102076589 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 363
    12  QuartzCore                          0x0000000102081956 -[CALayer layoutIfNeeded] + 162
    13  UIKit                               0x00000001003ebfc2 -[UIViewController window:setupWithInterfaceOrientation:] + 264
    14  UIKit                               0x000000010032ab4d -[UIWindow _setRotatableClient:toOrientation:updateStatusBar:duration:force:isRotating:] + 4360
    15  UIKit                               0x0000000100329a3f -[UIWindow _setRotatableClient:toOrientation:updateStatusBar:duration:force:] + 36
    16  UIKit                               0x000000010032998f -[UIWindow _setRotatableViewOrientation:updateStatusBar:duration:force:] + 101
    17  UIKit                               0x0000000100328c9e -[UIWindow _updateToInterfaceOrientation:duration:force:] + 377
    18  UIKit                               0x00000001003dfd4a -[UIViewController _tryBecomeRootViewControllerInWindow:] + 147
    19  UIKit                               0x0000000100323a87 -[UIWindow addRootViewControllerViewIfPossible] + 506
    20  UIKit                               0x0000000100323bd5 -[UIWindow _setHidden:forced:] + 275
    21  UIKit                               0x000000010032cca2 -[UIWindow makeKeyAndVisible] + 51
    22  UIKit                               0x00000001002eb0c8 -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 1449
    23  UIKit                               0x00000001002eebe8 -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 660
    24  UIKit                               0x00000001002ffaab -[UIApplication handleEvent:withNewEvent:] + 3092
    25  UIKit                               0x00000001002fff1e -[UIApplication sendEvent:] + 79
    26  UIKit                               0x00000001002f02be _UIApplicationHandleEvent + 618
    27  GraphicsServices                    0x0000000102578bb6 _PurpleEventCallback + 762
    28  GraphicsServices                    0x000000010257867d PurpleEventCallback + 35
    29  CoreFoundation                      0x00000001018ac819 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 41
    30  CoreFoundation                      0x00000001018ac5ee __CFRunLoopDoSource1 + 478
    31  CoreFoundation                      0x00000001018d5ab3 __CFRunLoopRun + 1939
    32  CoreFoundation                      0x00000001018d4f33 CFRunLoopRunSpecific + 467
    33  UIKit                               0x00000001002ee4bd -[UIApplication _run] + 609
    34  UIKit                               0x00000001002f0043 UIApplicationMain + 1010
    35  Liga Zon Sagres Companion           0x0000000100011f93 main + 115
    36  libdyld.dylib                       0x0000000102f815fd start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

任何想法如何解决这个问题?谢谢你。

rmaddy

该错误来自您的StandingsTableViewController tableView:cellForRowAtIndexPath:方法。您的数据为您提供了一个您希望获得的NSNull实例NSDictionary

由于您已显式添加NSNull对象,因此您需要更新cellForRow...方法以NSNull在假定它为之前检查该对象是否为实例NSDictionary

像这样的东西:

NSDictionary *data = self.tableData[someIndex];
if ([data isKindOfClass:[NSDictionary class]]) {
    // process the data as usual
} else {
    // This is probably the NSNull object, ignore it or handle appropriately
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Javascript | 将json对象添加到空json数组

来自分类Dev

将数组对象添加到对象

来自分类Dev

将元素添加到空数组

来自分类Dev

将值添加到对象数组

来自分类Dev

将SimpleXMLElement对象添加到数组

来自分类Dev

将PSVariable对象添加到数组

来自分类Dev

将PSCustom对象添加到数组

来自分类Dev

将数组对象添加到minimongo

来自分类Dev

将 var 添加到数组对象?

来自分类Dev

将 id 添加到对象数组

来自分类Dev

将多个对象添加到数组?

来自分类Dev

Typescript .push 函数将空对象添加到我的数组中

来自分类Dev

如何仅将非空数组添加到数组?

来自分类Dev

将空关联数组添加到数组?

来自分类Dev

将File对象添加到File对象数组

来自分类Dev

将数组对象添加到数组php?

来自分类Dev

将数组中的对象数组添加到 JSON 中

来自分类Dev

将JSON添加到空数组问题

来自分类Dev

将新项目添加到对象数组

来自分类Dev

敲除将javascript对象添加到可观察数组

来自分类Dev

将元素添加到作为对象属性的数组中

来自分类Dev

将数组添加到活动记录对象

来自分类Dev

将数组对象添加到HTML表

来自分类Dev

将键值对添加到javascript中的对象数组?

来自分类Dev

使用游标循环将JS对象添加到数组

来自分类Dev

如何将多个对象添加到数组

来自分类Dev

将值添加到对象PHP中的数组

来自分类Dev

将属性添加到JavaScript中的对象数组

来自分类Dev

将数组添加到现有对象