Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check in an NSAssert of an variable is (null)?

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 Answers

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)"

like image 105
Barry Wark Avatar answered Sep 07 '25 21:09

Barry Wark