JLT JLT - 1 year ago 62
Objective-C Question

Is Checking The Pointer Of An Object With Negation Operator The Same As With Comparing It To Nil?

I maybe naming it wrong, so please correct me.

I just want to know if this

//do something

is exactly the same as this

if(obj == nil)


I mean are they both checking whether the object points to nil?

Answer Source

Yes, this is exactly the same. ! operator turns anything other than nil into zero, while nil becomes 1. Therefore, you get exactly the same results as the comparison with nil.

Similarly, using if (obj) is logically identical to if (obj != nil). This convention is inherited from C, where pointers are commonly used in conditions, and NULL-checked with ! operator.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download