Differences between nil, NULL, Nil, and NSNull in iOS
1. nil
> Defines the id of a null instance.
Define an instance as null and point to the NULL pointer of the object in oc.
> Sample code:
NSString * someString = nil;
NSURL * someURL = nil;
Id someObject = nil;
If (anotherObject = nil) // do something
> It is best to assign a value of nil to a release object to ensure security. If nil is not assigned, the program may crash.
NSArray * array = [NSArray arrayWithObjects: @ "test", @ "test1", nil];
[Array release];
If (array)
{
// Only the array release is assigned with no nil value. If you continue to operate the array somewhere in the program, the program will crash directly.
NSString * string = [array objectAtIndex: 0];
NSLog (@ "% @", string );
}
2. NULL
> These macros define null values for classes and instances.
NULL can be used on various pointers in C language,
# Define _ DARWIN_NULL # define _ DARWIN_NULLConstants
> Sample code:
Int * pointerToInt = NULL;
Char * pointerToChar = NULL;
Struct TreeNode * rootNode = NULL;
> In Objective-C, the nil object is designed to be associated with NULL pointers. Their difference is that nil is an object, while NULL is only a value. In addition, the nil call method does not generate crash or throw an exception.
3. Nil
> Defines the id of a null class.
Define an empty class
Available in Mac OS x v10.0 through Mac OS X v10.4.
Declared in NSObjCRuntime. h.
Declared Inobjc. h
> Sample code:
Class someClass = Nil;
Class anotherClass = [NSString class];
4. NSNull
> The NSNull class defines a singleton object used to represent null values in collection objects (which don't allow nil values ).
The NSNull class defines a singleton object to indicate a null value of the collection object.
> A collection object cannot contain nil as its specific value, such as NSArray, NSSet, and NSDictionary. Correspondingly, the nil value is represented by a specific object NSNull. NSNull provides a single instance to represent the nil value in the object property. In the default implementation method, dictionaryWithValuesForKeys: And setValuesForKeysWithDictionary: automatically convert NSNull and nil. Therefore, you do not need to perform NSNull testing on your object.