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))

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 is used when nil is not possible.

Because nil has a special meaning in NSArray and NSDictionary (indicating the end of a list), you cannot put a nil value in a collection. If you really want to store a value that means “nothing,” you can use the NSNull class. NSNull has only one method: + (NSNull *) null;

Nil is used to assign to an object (any object in Objective-C is of type ID), NULL is used to assign to any pointer, NULL and nil are not interchangeable, nil is used to assign to a class pointer (in Objective-C a class is an object, an instance of a meta-class of a class), NSNull is used for collection operations, and while both represent null values, they are used in completely different contexts.

id object = nil; // Check that the object is not emptyif(object) {// Operate on object} // Check that the object is emptyif(object == nil) {} // Array initialization, null end NSArray *array = [[NSArray alloc] initWithObjects:@"A"The @"B", nil]; NSString *element = [array objectAtIndex:2];if ((NSNull *)element == [NSNull null]) 
{  
 }  
Copy the code

To determine whether an array element is empty, none of the following is valid

if(! element)if([element length]>0)

if(element== NULL)

if(element == Nil)
Copy the code

Determines whether the element of the dictionary object is empty

 NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:  
 @"A"The @"A"The @"B"The @"B", nil];  
 NSString *value = [dictionary objectForKey:@"A"];  
 if ((NSNull *)value == [NSNull null]) {    }
Copy the code

Conclusion:

Nil: normally assigned to an empty object; 2. NULL: Normally assigned to a value other than nil. Such as SEL; 3, NSNULL: NSNULL has only one method: + (NSNULL *) null; [NSNull null] is used to add non-nil (end-of-list) null values to NSArray and NSDictionary. [NSNull null] is an object used when nil cannot be used. 4. When a message is sent to nil, NO is returned, NO exception is raised, and the program continues. An exception is received when a message is sent to an object of NSNull. Because nil has a special meaning in NSArray and NSDictionary (indicating the end of a list), you cannot put a nil value in a collection. If you really want to store a value that means “nothing,” you can use the NSNull class. NSNull has only one method: + (NSNull *) null;

Nil is an object pointer that is NULL, nil is a class pointer that is NULL, and NULL is the basic data type that is NULL

From a technical implementation point of view, nil is the same as NULL. But this definition actually increases the readability of the code. If you pass someone a NULL value, then someone thinks they are receiving a C pointer. If it’s nil, you’re accepting an object. Nil says it accepts class. So what’s the difference between NSNull and nil? If an object obj is nil, then the call to [obj message] will not throw an NSException and will normally return NO; If an object obj is NSNull, [obj Message] will throw an NSException. Oc has a feature that returns 0 instead of raising an exception when sending a message to nil, which is quite different from JAVA’s annoying NullPointerExceptions and C/C++ programs that crash directly.