Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Nil and nil and Null in Objective-C [duplicate]

I am a relatively experienced iOS developer, however one thing has always bothered me, what is the difference between Nil(Capital) and nil(lowercase) and NULL (if any) in terms of performance and usage? I am absolutely sure that there is a difference between them otherwise why would they be defined as separately in the first place...

like image 442
virindh Avatar asked Sep 01 '13 03:09

virindh


People also ask

Is nil the same as NULL Ruby?

nil is an Object, NULL is a memory pointer Sadly, when this happens, Ruby developers are confusing a simple little Ruby object for something that's usually radically different in “blub” language. Often, this other thing is a memory pointer, sometimes called NULL, which traditionally has the value 0.

What does nil mean in C?

Objective-C builds on C's representation of nothing by adding nil . nil is an object pointer to nothing. Although semantically distinct from NULL , they are technically equivalent.

Why does Ruby use nil instead of NULL?

Well, "nil" is the traditional name for the reified concept of "nothing" in Lisp and Smalltalk†. The word "null" is used as an adjective meaning "empty", as in "the null list," which is nil. Meanwhile, "null" is traditionally a pointer value in C that signifies the pointer doesn't point to anything valid.

What is the difference between nil and none?

There is no difference, as . none (basically it's Optional. none ) and nil are equivalent to each other. The use of nil is more common and is the recommended convention.


1 Answers

The basic idea is that you use NULL for C pointers (0 for C primitives), nil for Objective C instances, Nil for Objective C classes, and NSNull as a way of pretending that nothing is something so that you can store an instance of nothing in an array or dictionary.

A great description of this and the source of the values listed below can be found at: http://nshipster.com/nil/

NULL (void *)0 literal null value for C pointers

nil (id)0 literal null value for Objective-C objects

Nil (Class)0 literal null value for Objective-C classes

NSNull [NSNull null] singleton object used to represent null

like image 192
Mick MacCallum Avatar answered Sep 22 '22 16:09

Mick MacCallum