I am not sure but i think nil should only be used in place of an id, what Java and C++ programmers would think of as a pointer to an object. Use NULL for non-object pointers.
nil is usually used for an Objective-C object type, while NULL is used for c-style pointers
So there is no difference logically. The main idea here is to initialize a pointer whether C or Objective-C to 0. If you have knowledge of C then you can assign
int *ptr = 0;
without type casting 0 to a pointer. As you don't need to typecast 0 to assign it to a pointer.
nil is the literal null value for Objective-C objects, corresponding to the abstract type id or any Objective-C type declared via @interface. For instance:
NSString *someString = nil;
NSURL *someURL = nil;
id someObject = nil;
if (anotherObject == nil) // do something
Nil is the literal null value for Objective-C classes, corresponding to the type Class. Since most code doesn’t need variables to reference classes, its use is not common. One example is:
Class someClass = Nil;
Class anotherClass = [NSString class];
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 from nil because nil is a literal null value, i.e., it isn’t an object. The single instance of NSNull, on the other hand, is a proper object.
NSNull is often used in Foundation collections since they cannot store nil values. In the case of dictionaries, -objectForKey: returns nil 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:
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 because nil cannot be stored in a Foundation collection. For instance,
This will help you to understand the difference between nil,NIL and null.
All three of these values represent null, or zero pointer, values. 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.
It should be considered a best practice of sorts to use the right null
object in the right circumstance for documentation purposes, even
though there is nothing stopping someone from mixing and matching as
they go along.