I want to know the difference between nil
, NIL
and null
.
I've googled around and found this:
nil
-> null pointer to Objective-C object
NIL
-> null pointer to Objective-C class
null
-> null pointer to primitive type or absence of data
But I'm not able to understand the terms "Objective-C object" and "class" clearly.
Please explain this to me. Also, is there any word like NSNull
or NSNil
in Objective-C? If so, then please explain for what it is for.
nil
is the literal null value for Objective-C objects, corresponding to the abstract typeid
or any Objective-C type declared via@interface
. For instance:Nil
is the literal null value for Objective-C classes, corresponding to the typeClass
. Since most code doesn’t need variables to reference classes, its use is not common. One example is:NULL
is the literal null value for arbitrary C pointers. For instance,NSNull
is a class for objects that represent null. In fact, there’s only one object, namely the one returned by+[NSNull null]
. It is different fromnil
becausenil
is a literal null value, i.e., it isn’t an object. The single instance ofNSNull
, on the other hand, is a proper object.NSNull
is often used in Foundation collections since they cannot storenil
values. In the case of dictionaries,-objectForKey:
returnsnil
to indicate that a given key has no corresponding object in the dictionary, i.e., the key hasn’t been added to the dictionary. If you want to make it explicit that you have a certain key but it doesn’t have a value yet, you can use[NSNull null]
.For instance, the following throws an exception because dictionaries cannot store
nil
values:On the other hand, the following code is valid since
[NSNull null]
is a non-nil
object:It’s worth mentioning that Foundation collections have initialisers that use
nil
as a marker for the end of a list of objects without having to specify the number of elements in the list. This can only happen becausenil
cannot be stored in a Foundation collection. For instance,As for
NIL
orNSNil
, there are no such things in Objective-C or Apple Foundation.This will help you to understand the difference between nil,NIL and null.
The below link may help you in some way:
http://nshipster.com/nil/
Here is some important part from the link:
I am not sure but i think
nil
should only be used in place of anid
, what Java and C++ programmers would think of as apointer
to anobject
. UseNULL
for non-object pointers.nil
is usually used for an Objective-C object type, whileNULL
is used for c-style pointersSuppose you have a class
MyClass
then by conventionnil
is used if you want to initialize its instance to null value (same asnull
in java) i.e.and if you want to initialize a primitive pointer to null value (same as in c) you use
and if you want to initialize to
Class
reference to null value (same asnull
in java) then useIt's just a convention otherwise Nil or nil have same meaning and perhaps NULL , nil or Nil all are same.
Here is the definition for these in
objc.h
fileAnd in
stddef.h
And the definition of
__DARWIN_NULL
in_types.h
So there is no difference logically. The main idea here is to initialize a pointer whether
C
orObjective-C
to0
. If you have knowledge ofC
then you can assignwithout type casting
0
to a pointer. As you don't need to typecast0
to assign it to a pointer.In short they all are
0
and nothing else.nil, NIL and null. is depended on your requirement.
NSNull
collections like
NSArray
andNSDictionary
not being able to contain nil values.nil
all pointers that object has to other objects begin as nil, so it's unnecessary to, for instance, set self.(association) = nil in init methods.
In other languages, like C++, this would crash your program, but in Objective-C, invoking a method on nil returns a zero value.
Symbol Value Meaning
nil (id)0 literal null value for Objective-C objects
Nil (Class)0 literal null value for Objective-C classes
Nil,Null and nil are used with below
1> Nil for Objective c Class
2> nil for Objective c object
3> Null for C pointer
Example:
1>Class A=Nil;
2>NSString strName=nil;
3>char *pointerChar = NULL;