The use of iOS development-nil/nil/null/nsnull

Source: Internet
Author: User
Tags define null

NIL:A null pointer to a Objective-c object. (#define NIL ((ID) 0))
Nil is an object value.

NIL:A null pointer to a OBJECTIVE-C class.

NULL:A null pointer to anything else. (#define NULL ((void *) 0))
Null is a generic pointer (generic pointer).

Nsnull:a class defines A Singleton object used to represent null values in collection objects (which don ' t allow Nil ES).
[Nsnull NULL]: The singleton instance of Nsnull.

[Nsnull NULL] is an object that he uses in situations where nil cannot be used.

Because there is a special meaning in nil in Nsarray and nsdictionary, which means the end of the list, you cannot put nil values in the collection. If you really need to store a value that says "Nothing," you can use the Nsnull class. Nsnull only One method: + (Nsnull *) null;

Nil is used to assign values to objects (any object in objective-c is of type ID), NULL assigns any pointers, null and nil are not interchangeable, nil is used for class pointer assignment (the class is an object in Objective-c, is an instance of the Meta-class of a Class), and Nsnull is used for collection operations, and although they represent null values, they are used in a completely different context.

  ID object = nil;   

  //array initialization, NULL value end
Nsarray *array = [[Nsarray alloc] initwithobjects:@ "A", @ "B", nil];

//To determine whether an array element is empty
 NSString *element = [array objectatindex:2];
 if ((Nsnull *) Element = = [Nsnull null])
{  
 }

To determine whether an array element is empty, the following writing is not valid

if (!element) if (

[element length]>0) if (

element== NULL)

if (element = = Nil)

Determines whether the element of a Dictionary object is empty

Nsdictionary *dictionary = [nsdictionary dictionarywithobjectsandkeys:
 @ "A", @ "a", @ "B", @ "B", nil];  
 nsstring *value = [Dictionary objectforkey:@ "A"];
 if ((Nsnull *) value = = [Nsnull null]) {  }

Summarize:

1, Nil: general assignment to empty objects;
2, NULL: general assignment to nil other than the null value. such as the SEL, etc.;
3, Nsnull:nsnull only one method: + (Nsnull *) null;
[Nsnull NULL] is used to add a null value in Nsarray and nsdictionary that is not nil (the end of the list). [Nsnull NULL] is an object used in situations where nil cannot be used.
4, when sending messages to the nil, return no, there will be no exception, the program will continue to carry out;
An exception is received when sending a message to an Nsnull object.
Because there is a special meaning in nil in Nsarray and nsdictionary, which means the end of the list, you cannot put nil values in the collection. If you really need to store a value that says "Nothing," you can use the Nsnull class. Nsnull only One method: + (Nsnull *) null;

Nil is an object pointer to null,
Nil is a class pointer is null,
Null is empty for base data type

Nil, NULL is the same for technical implementation. But this definition can actually increase the readability of the code. If you pass a null value to someone, then someone thinks the C pointer is received. If it is nil, then it is accepted as an object. and nil, it is considered to accept class.
So what is the difference between nsnull and nil?
How an object obj is nil, then invoking [obj message] will not throw out the nsexception exception and return no to normal;
If an object obj is Nsnull, then [obj message] Will throw out the nsexception exception.
OC has an attribute that, when sending a message to a nil object, the system returns a value of 0 instead of causing an exception, which is quite useful, unlike Java's annoying nullpointerexception, and the process of directly crashing a C + + program.

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.