How can I check NSAssert of a variable (null)?
the console prints me (null) for a variable that shouldn't be there, so I want to put a statement there just for fun:
NSAssert(myVar, @"myVar was (null)!");
What's the trick here? this (zero) thing doesn't seem to be "nil", right? How can I check this? I want to assume that var is set correctly, not nil, not null.
Assuming it myVar
has a type id
(or any type of object), use
NSAssert(myVar != nil, @"")
If this assertion passed, you should be able to print myVar
to the console with
NSLog(@"myVar = %@", myVar);
As a side note nil==null
(both meanings #defined
like __DARWIN_NULL
that #defined
((void *)0)
). If you try NSLog
to print -description
an object reference nil
(or NULL
), you get "(null)"
do [myVar isEqualToClass: [NSNull null]] returns yes if it's NSNull if it's nil you can do if (myVar == nil)
I usually do
if (object != nil)
doSomething(object);
else
NSLog("bad object!");