Adam Jenkins
Adam Jenkins

Reputation: 55792

Compare to null in objective c

I am beginning to find my code littered with:

if([p objectForKey@"somekey"] != [NSNull null]) {

}

Is there shorter (character-wise) comparison for NULL?

Background: I am using the SBJson library to parse a JSON string and there are often null values (by design) for some of the keys.

Upvotes: 8

Views: 3632

Answers (7)

James Webster
James Webster

Reputation: 32066

If you're just worried about the amount of time your taking to type, consider macros:

#define ISNULL(key) [p objectForKey:key] == [NSNull null]

then

if (!ISNULL(@"somekey")) ...

Upvotes: 0

Groot
Groot

Reputation: 14261

I would use

if([[p objectForKey@"somekey"] isEqual:[NSNull null]] || ![p objectForKey@"somekey"]) {
    // NSNull or nil
} else {
    // Stuff exists...Hurray!
}

It seem to work since [NSNull null] is in fact an "object". Hope it helps!

Upvotes: 5

Sulthan
Sulthan

Reputation: 130201

Since you are using SBJSON, you can easily change its code - you have the source.

I have actually modified SBJSON parser to skip [NSNull null] values. They are not added to the dictionaries and when I call objectForKey:, I never get [NSNull null], I just get nil. Then, in most situation I don't even have to check if the value is nil since calling a method on nil usually gives the result I expect.

Upvotes: 0

justin
justin

Reputation: 104708

Is there shorter (character-wise) comparison for NULL?

[NSNull null] is 13 chars. You can say:

NSNull.null // << 11
(id)kCFNull // << 11

Or make a function:

IsNSNull([p objectForKey@"somekey"]) // << 10 in this case and requires no ==, !=

Or (cringes) use a category:

[p objectForKey@"somekey"].mon_isNSNull // << 13 in this case, but requires no ==, !=

Just be careful how you name that category when dealing with nil receivers.

Upvotes: 1

Rob Napier
Rob Napier

Reputation: 299633

Nothing built-in, but it would be reasonable and simple to create a function MYIsNull() that would do the comparison you want. Just think through what you want to return in the case that the key is missing.

You may want to go the other way and transform -null into nil. For instance, you could add a category on NSDictionary like this:

- (id)my_nonNullObjectForKey:(NSString *)key {
   id value = [self objectForKey:key];
   if ([value isEqual:[NSNull null]) {
     return nil;
   }
   return value;
}

Upvotes: 7

Simon
Simon

Reputation: 26003

No, you have to test for NSNull. However, if you're finding your code is being littered by it, you might want to create a #define for it.

Bear in mind also that if p is nil, or if p doesn't have a value for someKey, then [p objectForKey@"somekey"] != [NSNull null] evaluates to YES.

So you probably want something like this:

#define IsTruthy(X) ( X && (X != [NSNull null]) )

Upvotes: 3

morningstar
morningstar

Reputation: 9162

Pretty sure you can just say

if ([p objectForKey:@"somekey"]) {

}

I don't use NSNull much so I'm not 100% sure but I think it tests as false and any other object tests as true.

Upvotes: -4

Related Questions