The difference between Nil, NULL, Nil, and Nsnull in iOS

Source: Internet
Author: User
Tags define null empty null null

1.nil

>defines the ID of a null instance.

Defines an instance that is empty, pointing to an empty pointer to an object in OC.

> Sample code:

NSString *somestring = nil;

Nsurl *someurl = nil;

ID someobject = nil;

if (Anotherobject = = nil)//do something

> When you release an object, it is best to assign them to nil, which ensures security, which can cause a crash if you do not assign a value of nil.

Nsarray * array = [Nsarray arraywithobjects:@ "test", @ "Test1", nil];

[Array release];

if (array)

{

Only the array release, not assigned to nil, somewhere in the program if you continue to operate the array, the program directly crashes

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 the C language,

#define __DARWIN_NULL #define__DARWIN_NULLConstants

> Sample code:

int *pointertoint = NULL;

char *pointertochar = NULL;

struct TreeNode *rootnode = NULL;

> in objective-c, nil objects are designed to be associated with NULL null pointers. The difference is that nil is an object, and null is just a value. And we call the method on the nil without generating crash or throwing an exception.

3.Nil

>defines the ID of a null class.

To 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 single instance object to represent the null value of a collection object

> Collection objects cannot contain nil as their specific values, such as Nsarray, Nsset, and Nsdictionary. Accordingly, the nil value is represented by a specific object nsnull. Nsnull provides a single instance to represent the nil value in an object property. The default implementation method, Dictionarywithvaluesforkeys: And setvaluesforkeyswithdictionary: automatically converts nsnull and nil to each other, so your object does not need to perform nsnull test operations.

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.