In objective-c is the end result of !variable the same as variable==nil Also I think I read somewhere that iVars are initialized to “nil” (i.e. o) but sadly I can’t seem to find where I spotted it now. If I am correct is this initialization to nil part of declaring an iVar or is it linked to something else like @property?
i.e. do these evaluate the same …
if(!myObject) ...
and
if(myObject == nil) ...
Cheers Gary.
Edited: hopefully for more clarity.
Your question subject and question body appear to be asking different things, so…
To answer the question subject: No,
!andnilare not at all the same.!is an operator that returns the logical negation of its operand (that is,!0returns1and!with anything else returns 0), whilenilis a macro for 0.To answer the question itself: Yes,
!fooandfoo == nilgive the same result for object variables. And yes, instance variables are initialized to 0. So are static variables and global variables.