views:

128

answers:

3

the console prints me (null) for an variable that should not be, so I want to put an assert there just for fun:

NSAssert(myVar, @"myVar was (null)!");

what's the trick here? this (null) thing doesn't seem to be "nil", right? How can I check for it? I want to assume that the var is set properly and not nil, not null.

+1  A: 

do [myVar isEqualToClass:[NSNull null]] this returns yes if it is NSNull, if its nil you can do if(myVar==nil)

Daniel
Just to be clear: NSNull is not the same as nil or null.
mahboudz
it is not, therefore i gave two ways to compare if
Daniel
+1  A: 

Assuming myVar is of type id (or any object type), use

NSAssert(myVar != nil, @"")

If the assertion passes, you should be able to print myVar to the console with

NSLog(@"myVar = %@", myVar);

As a side note, nil==null, (both are #defined as __DARWIN_NULL which is #defined ((void *)0)). If you try to use NSLog to print the -description of a nil (or NULL) object reference you get "(null)"

Barry Wark
A: 

I usually do

if (object != nil)
  doSomething(object);
else
  NSLog("bad object!");
mahboudz