Differences between nil, Nil, and NULL in iOS, iosnil
1. Description
Nil: a null pointer to an object in oc
Nil: NULL pointer to class in oc
NULL: pointing to other types of NULL pointers, such as a c-type memory pointer
NSNull: an object that represents a null value in a collection object.
If obj is nil:
[Obj message] NO instead of NSException will be returned
If obj is NSNull:
[Obj message] will throw an NSException
2. Usage
Nil and NULL are simple to understand. nil is an object and NULL is a value. I understand that nil is used to set the object to NULL, NULL sets the basic type to NULL. In addition, the nil call method does not generate crash or throw an exception.
NSURL *url = nil;Class class = Nil;int *pointerInt = NULL;
Nil is an object pointer with NULL values, Nil is a class pointer with NULL values, and NULL is a basic data type with NULL values.
These five concepts are easily obfuscated and have the same usage in some cases. First, explain the meaning of "Null Pointer". a null pointer indicates that the pointer does not point to a meaningful memory area. For example, int * p; int * p = NULL;
NSObject * obj1 = [[NSObject alloc] init]; NSObject * obj2 = [NSNull null]; NSObject * obj3 = [NSObject new]; NSObject * obj4; NSArray * arr1 = [NSArray arrayWithObjects: obj1, obj2, obj3, obj4, nil]; NSLog (@ "arr1 count: % ld", [arr1 count]); // count: 3 because obj = nil, NSObject * obj1; NSObject * obj2 = [[NSObject alloc] init]; NSObject * obj3 = [NSNull null] ends when obj4 is added; NSObject * obj4 = [NSObject new]; NSArray * arr2 = [NSArray arrayWithObjects: obj1, obj2, obj3, obj4, nil]; NSLog (@ "arr2 count: % ld ", [arr2 count]); // count: 0. Because obj1 = nil, the objects following it are not added.
[NSNull null] is usually used as a placeholder, as follows:
NSObject *obj1 = [NSNull null];NSArray *arr1 = [NSArray arrayWithObjects:@"One", @"TWO", obj1,@"three",nil];for (NSString *str in arr1) { NSLog(@"array object: %@", str); } //result:One、Two、、three NSObject *obj1 = [NSNull null];NSArray *arr1 = [NSArray arrayWithObjects:@"One", @"TWO", obj1,@"three",nil];for (NSString *str in arr1) { if (str != [NSNull null]){ NSLog(@"array object: %@", str); }}
Finally personal small site recommendations: http://www.iosask.com everyone to communicate and learn.