Discrimination Nil,nil,null,nsnull Concept Description:
Nil: Indicates that the pointer to the object in OC is empty
Nil: Indicates that the pointer to the class in OC is empty
NULL: Pointer to other type is null, such as a C type of memory pointer
NSNull: is a simple interest object that represents an empty value in a collection that does not allow the object pointer to be nil
If obj is nil:
[obj message] will return no, not nsexception
If obj is nsnull:
[obj message] throws an exception nsexception
Discrimination Nil,null,nil
Nil and Null are literally understood to be simpler, nil is an object, and null is a value, and my understanding of nil is to set the object to null, point to address 0, and Null to set the base type pointer to null. And we're not going to generate crash or throw exceptions for the nil call method.
Take a look at the usage
Nsurl *url = nil;
Class class = Nil;
int *pointerint = NULL;
Nil is an object pointer is empty, nil is a class pointer is null, NULL is the basic data type pointer is empty.
These five concepts are easy to confuse, and in some cases they have the same usage. First explain the meaning of the null pointer, which means that the pointer does not point to a meaningful memory area. such as int *p; int *p = NULL;
The following code looks at how the Nil pointer object and the Nsnull are different when the array is added.
NSObject *obj1 =[[NSObject alloc] init]; NSObject*obj2 = [NSNullNULL]; NSObject*OBJ3 = [NSObjectNew]; NSObject*Obj4; Nsarray*ARR1 =[Nsarray arraywithobjects:obj1, Obj2, Obj3, Obj4, nil]; NSLog (@"arr1 count:%ld", [arr1 Count]);//Count:3 because Obj=nil, add objects when you join Obj4NSObject*obj1; NSObject*obj2 =[[NSObject alloc] init]; NSObject*OBJ3 = [NSNullNULL]; NSObject*OBJ4 = [NSObjectNew]; Nsarray*ARR2 =[Nsarray arraywithobjects:obj1, Obj2, Obj3, Obj4, nil]; NSLog (@"arr2 Count:%ld", [arr2 Count]);//count:0, because Obj1=nil, so the object is not added in the following[NSNullNULL] usually acts as a placeholder, as follows: NSObject*obj1 = [NSNullNULL]; Nsarray*ARR1 = [Nsarray arraywithobjects:@" One",@" Both", Obj1,@"three", nil]; for(NSString *strincharr1) {NSLog (@"Array object:%@", str);} //Result:one, three , andNSObject*obj1 = [NSNullNULL]; Nsarray*ARR1 = [Nsarray arraywithobjects:@" One",@" Both", Obj1,@"three", nil]; for(NSString *strincharr1) {if(str! = [NSNullNULL]) {NSLog (@"Array object:%@", str);}}//Result:one, three ,
Discrimination Nil,nil,null,nsnull