Tyler DeWitt
Tyler DeWitt

Reputation: 23576

Are kCFNull and [NSNull null] Interchangeable

It seems to be

if(myObj == (typeOfMyObj *) kCFNull){
 //myObj is null
}

and

if(myObj == (typeOfMyObj *) [NSNull null]){
 //myObj is null
}

produce the same result.

Is this always the case? I'm developing an iOS 5 application.

Thanks!

Upvotes: 3

Views: 864

Answers (2)

Steazy
Steazy

Reputation: 393

I agree with Jeremy,

If you wanted to be 100% safe, the best bet would be to check the class using isKindOfClass:

ie:

[object isKindOfClass:[NSNull class]]

or

[object isEqual:[NSNull null]]

Then you're safe against any future changes, though it's super tedious and the slowest of all proposed approaches :P

It's not out of the question that one day

[[NSNull null] isEqual:(id)kCFNull] == YES

but

([NSNull null] == kCFNull) == NO

or even

([NSNull null] == [NSNull null]) == NO

Though these are the sorts of concerns that cause most Obj-C devs to spend more time jumping through hoops than writing code :P

Upvotes: -2

Jeremy W. Sherman
Jeremy W. Sherman

Reputation: 36143

It appears none of Apple's docs state that NSNull and CFNullRef are toll-free-bridged, but it nevertheless seems that they are and have been since CFNullRef was introduced in Mac OS X 10.2. NSNull was available starting with Mac OS X 10.0. In the end, they are both really just sentinel values that serve as a distinguished NULL object.

There's probably a way for your code to avoid depending on kCFNull == [NSNull null], but if you can't for some reason, then I wouldn't worry about it too much.

Upvotes: 5

Related Questions