The difference between nil,nil,null in iOS

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.