Nil Nil NULL NSNull
NIL:A null pointer to an OBJECTIVE-C object. (#define NIL ((ID) 0))
Nil is an object value.
NIL:A null pointer to an OBJECTIVE-C class.
NULL:A null pointer to anything else. (#define NULL ((void *) 0))
Null is a general-purpose pointer (generic pointer).
Nsnull:a class defines A Singleton object used to represent null values in collection objects (which don ' t allow Nil values).
[NSNull NULL]: The singleton instance of NSNull.
[NSNull NULL] is an object that he uses in situations where nil can not be used.
Because there is a special meaning in nil in Nsarray and nsdictionary (indicating the end of the list), you cannot put a nil value in the collection. If you really need to store a value that says "Nothing", you can use the Nsnull class. There is only one way to Nsnull:
+ (NSNull *) null;
Technically they ' re all the same, but in practice they give someone reading your code some hints about what ' s going on; Just like naming classes with a capital letter and instances with lowercase are recommended, but not required.
If someone sees you passing NULL, they know the receiver expects a C pointer. If They see nil, they know the receiver was expecting an object. If They see Nil, they know the receiver is expecting a class. Readability.
Example 1:
[Nsapp Beginsheet:sheet
Modalforwindow:mainwindow
Modaldelegate:nil//pointing to an object
Didendselector:null//pointing to a non object/class
Contextinfo:null]; Pointing to a non object/class
Example 2:the values1 is only having obj1,
NSObject *obj1 = [NSObject new];
NSObject *obj2 = nil;
NSObject *obj3 = [NSObject new];
Nsarray *values1 = [Nsarray arraywithobjects:obj1,obj2,obj3,nil];
Example 3:the Values2 has 3 objects and the second object is instance of Nusnull
NSObject *obj1 = [NSObject new];
NSObject *obj2 = [NSNull null];
NSObject *obj3 = [NSObject new];
Nsarray *values2 = [Nsarray arraywithobjects:obj1,obj2,obj3,nil];
About testing:
If obj is nil, [obj message] would return NO, without nsexception
If obj is NSNull, [obj message] would throw a nsexception
Testing NSNull: (NSNull *) obj = = [NSNull null]