Ali
Ali

Reputation: 643

Kind of null variable IOS

i am actually debugging my iOS Application: i want to test if a variable is null or not:

Stupid question huh !

if (var !=nil) { doSomeWork ; }

So if the variable var is equal to nil and we want to print this result in the debugger area we will have something like that:

2012-10-12 21:33:01.553 Application's Name [892:13d03] (null)

This is cool, but indeed when i try to print the variable content in the debugger area, it has been showing :

2012-10-12 21:33:01.553 Application's Name [892:13d03] < null >

Can you tell me guys what is the difference between this two kinds of null, and how can i test if the second one is equal to nil.

Thanks in advance

Upvotes: 3

Views: 965

Answers (2)

NeverBe
NeverBe

Reputation: 5038

NSNull it is class, nil is not. So if you are comparing nil with something you should use "==", if NSNull then -> if ([var isEqual:[NSNull null]]) { ....}

Upvotes: 0

Lily Ballard
Lily Ballard

Reputation: 185721

The second output, <null>, comes from the NSNull singleton. This is a class called NSNull that has a class method +null that returns the same singleton instance of NSNull every time. The primary purpose of this class it to be able to act as a stand-in for nil in places where you can't put nil, such as in collections. For example, JSON libraries typically return NSNull when the JSON includes null.

You can test for this simply by asking if it's == to [NSNull null] (since it's a singleton), or possibly if [obj isKindOfClass:[NSNull null]]. You could use [obj isEqual:[NSNull null]] if you like. You could even ask if it's == kCFNull if you want, since CFNull and NSNull are toll-free bridged. Whatever style you want is up to you.

Upvotes: 6

Related Questions