user4951
user4951

Reputation: 33138

What's the Difference Between nil and Nil

In Objective C?

Are they really the same thing?

How to test that an object is nil?

Upvotes: 27

Views: 10956

Answers (3)

Carlos Avalos
Carlos Avalos

Reputation: 247

  • nil (all lower-case) is a null pointer to an Objective-C object.
  • Nil (capitalized) is a null pointer to an Objective-C class.
  • NULL (all caps) is a null pointer to anything else.

Upvotes: 3

JeremyP
JeremyP

Reputation: 86691

nil is the Objective-C constant for a null pointer to an object, Nil is identically defined. In practice, it is has the same value as the C constant NULL. Test for a nil object like this:

if (fooObj == nil)

In my code, I tend to use nil for Objective-C objects and NULL for other C pointers. This is a matter of personal taste - currently nil and NULL are interchangeable for comparison in all existing Objective-C implementations.

Upvotes: 10

Itai Ferber
Itai Ferber

Reputation: 29928

Nil and nil are defined to be the same thing (__DARWIN_NULL), and are meant to signify nullity (a null pointer, like NULL). Nil is meant for class pointers, and nil is meant for object pointers (you can read more on it in objc.h; search for Nil). Finally, you can test for a null value like this:

if (object == nil)

or like this:

if (!object)

since boolean evaluations will make any valid pointer that contains an object evaluate to true.

Upvotes: 51

Related Questions