Nil, nil, null, nsnull, [nsnull null] in objective-C

Source: Internet
Author: User

Many beginners are confused about nil, nil, null, nsnull, and [nsnull null! First, use the online explanation:

Nil: a null pointer to an objective-C object. pointer.

Nil: a null pointer to an objective-C class. pointer.

Null: a null pointer to anything else. (primitive type or absence of data) Conflict)

Nsnull: A class defines a singleton object used to represent null values in collection objects (which don't allow nil values. Nil mean the end of the collection). Specify ).

[Nsnull null]: The Singleton instance of nsnull. Enabled.

Technically they're all the same, but in practice they give someone reading your code some hints about what's going on; just like naming classes with a capital letter and instances with lowercase is recommended, but not required. else if someone sees you passing
Null, they know the specified er expects a C pointer. if they see nil, they know the specified er is expecting an object. if they see nil, they know the handler er is expecting a class. readability;
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.
Check usage
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;        if (obj1 != nil) {            NSLog(@"object is not nil");        }else{            NSLog(@"object is nil");        }        testClass *c1;        if (c1 != Nil) {            NSLog(@"class is not Nil");        }else{            NSLog(@"class is Nil");        }
        int *money;        if (money != NULL) {            NSLog(@"money is not NULL");        }else{            NSLog(@"money is 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、<NULL>、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);            }        }//result:One、Two、three
Related Article

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.