Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 863
user4951 Avatar asked May 10 '11 10:05

user4951


People also ask

What is difference between nil and null?

In short they all are 0 and nothing else. The difference is that while NULL represents zero for any pointer, nil is specific to objects (e.g., id) and Nil is specific to class pointers.

Is nil same as?

Nil is for object pointers, NULL is for non pointers, Null and Nil both defined to be equal to the value zero. NULL is a void *, nil is an id, and Nil is a Class pointer, NULL is used for non-object pointer (like a C pointer) in Objective-C.

Which is correct Nill or null?

Null : invalid or having a value of zero Nill : archaic for refuse (Shakespeare) ☝But I think you were referring to nil spelled with one L. 👇 Nil : zero, nothing, or nonexistent Examples: This contract is null and void. They beat us three to nil.

What is difference between null and nothing?

Null is a specific subtype of a Variant. It has no existence outside of the Variant type, and is created to allow a Variant to model a database null value. Nothing is a value of an Object variable. It essentially is identical to a null pointer, i.e. there is no object.


2 Answers

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.

like image 55
Itai Ferber Avatar answered Sep 21 '22 18:09

Itai Ferber


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.

like image 35
JeremyP Avatar answered Sep 23 '22 18:09

JeremyP