views:

153

answers:

4

I'm curious why a variable overtly assigned to nil, prints as (null) with NSLog:

NSString *myVar = nil;
NSLog(@"%@", myVar);

# RESULT: ' (null) '

This is of course quite confusing given all the different kinds of "nothingness" to figure out in Objective-C, and had me trying to test various IF NULL syntaxes.

+1  A: 

It's just the implementation of NSLog IMHO.

Adrian Kosmaczewski
A: 

This is what %@ format does, it casts nil to NSNull. myVar itself is still nill. You can still use if (myVar) for testing.

Tianzhou Chen
+1  A: 

(null) is the string representation of 'nil' for printing purposes... nothing related to IF NULL checks. myVar is still nil

psychotik
+5  A: 

The different kinds of "nothingness" summed up:

nil  //Null pointer to an Objective-C object
Nil  //Null pointer to an Objective-C class
NULL  //Null pointer

All of the above are defined as ((void *)0).

weichsel