当某些 json 为空时,即使我尝试先检查以下代码,我也会收到以下代码错误:
编辑:前面的代码:
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];
}
有没有更好的方法来检查 Json 不为空?
这是错误输出:
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'