The use of iOS nil,null,nsnull

Source: Internet
Author: User

When doing the project today, in the array inside the value, found that there are nsnull objects, and then use the corresponding object in the array to assign values when there are various problems, always error. After studying and checking the data, finally solved the problem.

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

1, Nil: general assignment to empty objects;

2, NULL: general assignment to a null value other than nil. such as SEL, etc.;

[Nsapp Beginsheet:sheet
Modalforwindow:mainwindow

Modaldelegate:nil//pointing to an object

Didendselector:null//pointing to a non object/class

Contextinfo:null]; Pointing to a non object/class

3, Nsnull:nsnull only one method: + (NSNULL *) null;

[NSNull NULL] is used to add a non-nil (representing the end of the list) to a null value in Nsarray and Nsdictionary. [NSNull NULL] is an object that he uses in situations where nil can not be used.

4, when sending messages to nil, return no, there will be no exception, the program will continue to execute;

You receive an exception when you send a message to an Nsnull object.

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

When I was doing the project today, I had to judge if the array element is empty, and my following notation is invalid if (!element) if ([element length]>0) if (element== NULL) if (element = = Nil) The correct way to determine whether an array and a dictionary element is empty is:

Determines whether the inside element of an array object is empty

NSString *element = [array objectatindex:2];

if ((NSNull *) Element = = [NSNull null]) {

}

Determines whether the inside element of the Dictionary object is empty

Nsdictionary *dictionary = [nsdictionary Dictionarywithobjectsandkeys: @ "IPhone" @ "First" @ "IPad" @ "Second", nil];

    1. NSString *value = [Dictionary objectforkey:@ "First"];
    2. if ((NSNull *) value = = [NSNull null]) {
    3. }

The use of iOS nil,null,nsnull

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.