Differences between nil, nil, nsnull, and null

Source: Internet
Author: User

I. Nil

When assigning values to an object, we usually use object = nil, indicating that I want to release this object;

Or for some reason, after multiple release operations, the Reference Counter of the object is 0, and the system releases the memory. At this time, the object is nil, I call it an "Empty object ". (Note: Here I emphasize "Empty object". Next I will compare it with "an object with null value !!!)

Therefore, all retain operations on such empty objects will cause program crashes, such as Dictionary addition of key values or array addition of new elements.

Ii. nsnull

The difference between nsnull and nil is that nil is an empty object and has completely disappeared from the memory. If we want to express "we need to have such a container, but there is nothing in this container. We use nsnull, which is called an "Empty object ". If you read the development documentation, you will find that the nsnull class inherits nsobject and only has one "+ (nsnull *) NULL;" class method. This indicates that the nsnull object has a valid memory address, so any reference to it in the program will not cause the program to crash.

Iii. Nil

Nil and nil are not strictly limited in terms of use. That is to say, nil can be used in all places where NIL is used, and vice versa. In the programmer's conventions, we define nil as an empty object, and nil as an empty class. The reference code is as follows:

Nil, nil, null, and nsnull

Iv. null

We know that object-C comes from C and supports C, which is also different from C. Null is a typical C language syntax. It represents a null pointer. The reference code is as follows:

Int * ponit = NULL;


Differences between nil, nil, nsnull, and null

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.