John
John

Reputation: 11

Objective-C: NSLog prints (null) for a var assigned to nil?

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.

Upvotes: 1

Views: 3462

Answers (4)

Adrian Kosmaczewski
Adrian Kosmaczewski

Reputation: 7966

It's just the implementation of the method NSLog IMHO.

Upvotes: 2

Tianzhou
Tianzhou

Reputation: 1008

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

Upvotes: 2

Thomas Zoechling
Thomas Zoechling

Reputation: 34253

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

Upvotes: 9

psychotik
psychotik

Reputation: 39019

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

Upvotes: 4

Related Questions