Are NULL and nil equivalent? Are NULL and nil equivalent? objective-c objective-c

Are NULL and nil equivalent?


nil and NULL are essentially the same, nil is something like (NSObject *)0, while NULL is more like (void *)0. But both are pointers with an integer value of zero. You can send messages to nil without raising an error.

NSNull and NULL (or nil, of course) are different things, however. You just use NSNull as a helper to add an empty object to an NSArray or another container class, since you can't add nil to them. So instead, you use [NSNull null] as a replacement, and you have to check if an array element is NSNull, not if it's nil (it will never be equal to nil).


From http://www.iphonedevsdk.com/forum/iphone-sdk-development/34826-nil-vs-null.html

nil and NULL are 100% interchangeable.

From:

  • NULL is for C-style memory pointers.
  • nil is for Objective-C objects.
  • Nil is for Objective-C classes.

Whenever you're writing Objective-C code, use nilWhenever you're writing C code, use NULL

But ultimately they're all defined as the same thing -- (void *)0, I think -- so in practice it doesn't really matter.


The concept is the same, with the difference that it's valid to send messages (call method) to nil.

NSNull is a real (singleton) class, that can be used for arrays or dictionnaries, who don't accept NULL or nil values.