Differences between nil/null/nsnull

Source: Internet
Author: User

Null is defined in C, but C does not include nil, nil, and nsnull. These three are part of objective-C (hereinafter referred to as OC.

    • What is null?

Int * A = NULL;

A null pointer to anything else, is for C-style memory pointers.
It is a pointer variable of the C type. It is a null pointer and does not point to any place. Its value in memory is set to 0. For example, if int * pointer = NULL, the pointer variable pointer has a value of 0 in the memory and does not point to any place.

Essence: NULL pointer

 

    • What are nil and nil?

      Nsobject * OBJ = nil;
      Nil: a null pointer to an objective-C object. (NULL pointer of the OC object .)

Class * class = nil;
Nil: a null pointer to an objective-C class. (NULL pointer of the OC class .)

Essence: NULL pointer. They are null at all.

 

    • What is nsnull?

[Nsarray arraywithobjects: @ "Steve Jobs", [nsnull null], @ "Tim Cook", nil];

Official explanation:NsnullClass defines a singleton object used to represent null values in collection objects (which don't allowNilValues ).
Nsnull inherits from nsobject. There is only one method [nsnull null]. When this method is called, a space is opened in the memory and the first address pointer of the space is returned. It has only one purpose, that is, it represents an empty object in the Set object, and plays a role in the trap;

Nature: Empty objects

 

    • Summary:

(1) null, nil, and nil are essentially empty pointers. The following test code:
If (null = nil & null = nil)
Nslog (@ "null = nil & null = nil ");
Else
Nslog (@ "null! = Nil | null! = Nil ");
Console print result: "null = nil & null = nil ".

(2) What is the difference between a null pointer and a null object.
The latter points to the nsobject object opened in the heap memory, and the former does not point to any.

(3) Use null, nil, and nil encoding correctly.

Nsobject * OBJ = NULL;
Int * A = nil;
Class * class = nil;
If your code misuse null, nil, and nil like this, you are a programmer with no tricks!

Author's statement: if any error occurs, please point it out. Thank you!

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.