I am getting an error with the following code when some json is null even though I am trying to check that first:
Edit: Preceding Code:
NSData* data = [NSData dataWithContentsOfURL: kItemsURL];
//previous line grabed data from api.
if (data) {
[self performSelectorOnMainThread:@selector(fetchData:) withObject:data waitUntilDone:YES];
}
- (void)fetchData:(NSData *)jsonFeed {
NSError* error;
NSDictionary* json = [NSJSONSerialization JSONObjectWithData:jsonFeed options:kNilOptions error:&error];
//Original code provided
if (![[json objectForKey:@"items"] isKindOfClass:[NSNull class]]) {
NSLog(@"got here");
NSLog(@"json%@",json);
latestItems = [[json objectForKey:@"items"]mutableCopy];
}
Is there a better way to check Json is not null?
Here is error output:
2016-05-03 13:05:43.820 testApp[407:60b] got here
2016-05-03 13:05:43.821 testApp[407:60b] json{
items = "<null>";
}
NSNull mutableCopyWithZone:]: unrecognized selector sent to instance 0x3ac26a70
2016-05-03 13:05:43.825 ChallengeU[407:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSNull mutableCopyWithZone:]: unrecognized selector sent to instance 0x3ac26a70'