Differences between nil, Nil, and NULL in iOS, iosnil

Source: Internet
Author: User

Differences between nil, Nil, and NULL in iOS, iosnil

1. Description

Nil: a null pointer to an object in oc

Nil: NULL pointer to class in oc

NULL: pointing to other types of NULL pointers, 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] NO instead of NSException will be returned

 

If obj is NSNull:

[Obj message] will throw an NSException

 

2. Usage

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.

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 = [[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、、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);  }}

  

Finally personal small site recommendations: http://www.iosask.com everyone to communicate and learn.

 

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.