将空对象添加到数组

时间:2014-02-04 02:32:46

标签: ios objective-c nsmutablearray unrecognized-selector

我需要将一个空对象添加到索引0,并使用来自第三方XML Feed的数据填充数组的索引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

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

1 个答案:

答案 0 :(得分:2)

错误来自您的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
}