1.
Nil: null pointer to object in OC
Nil: null pointer to class in OC
NULL: Pointer to a different type of null pointer, 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] will return no, not nsexception
If obj is nsnull:
[obj message] throws an exception nsexception
2.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, and NULL to set the base type 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 empty, NULL is the basic data type 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;
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, add objects when you join Obj4
NSObject *obj1;
NSObject *obj2 = [[NSObject alloc] init];
NSObject *obj3 = [NSNull null];
nsobject *obj4 = [NSObject new];
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
[NSNull null] usually acts as a placeholder, as follows:
NSObject *obj1 = [NSNull null];
Nsarray *arr1 = [Nsarray arraywithobjects:@"One", @"One", obj1,@" Three ", nil];
for (NSString *str in arr1) {
NSLog (@"Array object:%@", str);
} //result:one, three, and
NSObject *obj1 = [NSNull null];
Nsarray *arr1 = [Nsarray arraywithobjects:@"One", @"One", obj1,@" Three ", nil];
for (NSString *str in arr1) {
if (str! = [NSNull null]) {
NSLog (@"Array object:%@", str);
}
}//result:one, three, and
The difference between nil,nil,null in iOS