1

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'  
zztop
  • 701
  • 1
  • 7
  • 20
  • Assign `[json objectForKey:@"items"]` to a variable. Log that variable's `class`. Update your question with these changes and that output. Use that variable in the `if` statement and the call to `mutableCopy`. – rmaddy May 03 '16 at 20:22
  • It is a dictionary: json class__NSCFDictionary. I have added the preceding code above. – zztop May 03 '16 at 21:03
  • That's not what I asked. We know `json` is a dictionary from your original question. What is the result of `[json objectForKey:@"items"]`? – rmaddy May 03 '16 at 21:06
  • it is null. I will try testing the if null after putting it in a string. – zztop May 03 '16 at 21:08
  • No, it is not `null`. And don't put it in a string. Assign it to a variable and log it like this: `id something = json[@"items"]; NSLog(@"it's a %@", [something class]);`. – rmaddy May 03 '16 at 21:12
  • It logs as: json{ items = ""; Following your code exactly gives: it's a (null) – zztop May 03 '16 at 21:23

4 Answers4

0
if (![json[@"items"] isEqual:[NSNull null]]) {
//do your stuff in here

}
rounak
  • 9,217
  • 3
  • 42
  • 59
0

I don't know what's wrong with your code, but the easiest way to check for a JSON null value if you are sure that json is a dictionary is:

if (json [@"items"] != [NSNull null]) { ... }

[NSNull null] always returns the same instance of NSNull. There is never more than one instance of NSNull, so you can actually use pointer comparison to check whether an object is an NSNull instance.

gnasher729
  • 51,477
  • 5
  • 75
  • 98
-1
if (json && [json isKindOfClass:[NSDictionary class]]) {
    //your code here
} else {
    //invalid json
}
ayushgp
  • 4,891
  • 8
  • 40
  • 75
iOS77
  • 522
  • 5
  • 3
-2
if ([json objectForKey:@"items"] != nil) {
        NSLog(@"got here");
        NSLog(@"json%@",json);
        latestItems = [[json objectForKey:@"items"]mutableCopy];
}

NULL or nil is not a class, but a convention. The memory adress 0 is the point where a cpu starts to execute code during cold start. Therefore no object can have this address in memory.

By the book should the json not have the key @"items" if the value of @"items" is NULL. The missing key indicates a NULL value.

Helge Becker
  • 3,219
  • 1
  • 20
  • 33
  • Please lookup the documentation for the class NSNull. JSON documents contain null values. Explicit null values, Which are translated to [NSNull null], not nil or NULL. – gnasher729 May 03 '16 at 22:48
  • And the first line of code is nonsense. objectForKey _never_ returns nil, unless json itself is nil. – gnasher729 May 03 '16 at 22:49
  • @gnasher729 Incorrect. `objectForKey:` will return `nil` if there is no such key in the dictionary. But that has nothing to do with whether this particular dictionary has an `NSNull` value stored for the key. – rmaddy May 04 '16 at 02:15