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

Source: Internet
Author: User
Tags define null

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

1. nil

> Defines the id of a null instance.

Define an instance as null and point to the NULL pointer of the object in oc.

> Sample code:

NSString * someString = nil;

NSURL * someURL = nil;

Id someObject = nil;

If (anotherObject = nil) // do something

> It is best to assign a value of nil to a release object to ensure security. If nil is not assigned, the program may crash.

NSArray * array = [NSArray arrayWithObjects: @ "test", @ "test1", nil];

[Array release];

If (array)

{

// Only the array release is assigned with no nil value. If you continue to operate the array somewhere in the program, the program will crash directly.

NSString * string = [array objectAtIndex: 0];

NSLog (@ "% @", string );

}

2. NULL

> These macros define null values for classes and instances.

NULL can be used on various pointers in C language,

# Define _ DARWIN_NULL # define _ DARWIN_NULLConstants

> Sample code:

Int * pointerToInt = NULL;

Char * pointerToChar = NULL;

Struct TreeNode * rootNode = NULL;

> In Objective-C, the nil object is designed to be associated with NULL pointers. Their difference is that nil is an object, while NULL is only a value. In addition, the nil call method does not generate crash or throw an exception.

3. Nil

> Defines the id of a null class.

Define an empty class

Available in Mac OS x v10.0 through Mac OS X v10.4.

Declared in NSObjCRuntime. h.

Declared Inobjc. h

> Sample code:

Class someClass = Nil;

Class anotherClass = [NSString class];

4. NSNull

> The NSNull class defines a singleton object used to represent null values in collection objects (which don't allow nil values ).

The NSNull class defines a singleton object to indicate a null value of the collection object.

> A collection object cannot contain nil as its specific value, such as NSArray, NSSet, and NSDictionary. Correspondingly, the nil value is represented by a specific object NSNull. NSNull provides a single instance to represent the nil value in the object property. In the default implementation method, dictionaryWithValuesForKeys: And setValuesForKeysWithDictionary: automatically convert NSNull and nil. Therefore, you do not need to perform NSNull testing on your object.

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.