Domanda

I need to add an empty object to index 0 and index 1 of an array being populated with data from a third party XML feed.

This is my parseXML method, it works.

  -(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];
}

But when i run this i get a crash with the console output:

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

Any ideas how to solve this? Thank you.

È stato utile?

Soluzione

The error is coming from your StandingsTableViewController tableView:cellForRowAtIndexPath: method. Your data is giving you an NSNull instance where you expect an NSDictionary.

Since you explicitly add the NSNull object you need to update your cellForRow... method to check to see if the object is an NSNull instance before assuming it is an NSDictionary.

Something like this:

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
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top