I maybe naming it wrong, so please correct me.
I just want to know if this
if(!obj)
{
//do something
}
is exactly the same as this
if(obj == nil)
{
}
I mean are they both checking whether the object points to nil?
I maybe naming it wrong, so please correct me.
I just want to know if this
if(!obj)
{
//do something
}
is exactly the same as this
if(obj == nil)
{
}
I mean are they both checking whether the object points to nil?
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.