An explanation of nil, NULL, nil, nsnull in iOS (i)

Source: Internet
Author: User

1.nil
>defines the ID of a null instance.
Defines a null pointer to an object in OC that has an empty instance.
> Sample code:
NSString *somestring = nil;
Nsurl *someurl = nil;
ID someobject = nil;
if (Anotherobject = = nil)//do something

> When it's time to release an object, it's best to assign them to nil, which ensures security, and if you don't assign nil, it can cause the program to crash.
Nsarray * array = [Nsarray arraywithobjects:@ "test", @ "Test1", nil];
[Array release];

if (array)
{
Only an array of release, and no assignment to nil, somewhere in the program if you continue the array operation, 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, the nil object is designed to be associated with a NULL null pointer. The difference is that nil is an object, and null is just a value. And we're not going to generate crash or throw exceptions for the nil call method.

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 singleton object used to represent the null value of a collection object

> Collection objects cannot contain nil as their specific value, such as Nsarray, Nsset, and Nsdictionary. Accordingly, the nil value is represented by a specific object nsnull. Nsnull provides a single instance for representing the nil value in the object's properties. 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.

An explanation of nil, NULL, nil, nsnull in iOS (i)

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.