Discrimination Nil,nil,null,nsnull

Source: Internet
Author: User

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

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.