[Swift, OC] Talk about the difference between nil, nil, NULL, Nsnull

Source: Internet
Author: User

We all know that nil, nil, NULL, nsnull are all empty but what is the difference, I believe that we have little attention. Today we're going to talk a little bit about the difference between these few empty

1. Nil

Nil generally refers to the empty object, which is completely empty, and is completely freed from memory.

2. Nil

Nil and nil are basically no different, or you can use nil wherever you can, and vice versa. But as a procedural ape, we should be more rigorous. The difference between nil and nil is that nil represents an empty object, and two nil indicates that a class is empty.

3, NULL

Everyone knows that OC is based on C, and OC is fully compatible with C, and null originates from C, which represents a null pointer.

Both: int *p = NULL

4, NSNull

Nsnull is very interesting, we generally think, nsnull is also empty, but look at this goods is "NS" beginning very much like an object, essentially Nsnull is indeed an object, he inherited from NSObject. So what's the difference between it and nil? Nil is the complete release of an object, which is completely freed from memory. But when I want to empty an object but want a container, we can use Nsnull. Like a bottle of mineral water, we don't want the water inside, but we want to keep the bottle. Look at the differences in the code

Swift

Define an array

Let Mutablearray = Nsmutablearray ()

Define a dictionary

Let mutabledictionary = Nsmutabledictionary ()

Define Nsnull

Let null = NSNull ()

Add to Array

Mutablearray.addobject (NULL)

Add to Dictionary

Mutabledictionary.setobject (NULL, Forkey: "null")

Adding a nsnull program is normal because Nsnull is a class with memory addresses, so adding to dictionaries and arrays does not collapse. Here's nil.

Add Nil to array

Mutablearray.addobject (Nil)

Add nul to Dictionary

Mutabledictionary.setobject (Nil, Forkey: "Nill")

Direct error, because the array and dictionary are stored in objects, objects are address. But Nil does not have an address in memory, so direct error

The following is the OC version, with OC Friends can refer to:

-(void) Viewdidload {

[Super Viewdidload];

Nsmutablearray *mutablearray = [Nsmutablearray array];

Nsmutabledictionary *mutabledictionary = [Nsmutabledictionary dictionary];

NSNull *null = [NSNull null];

Use Nsnull, no error will run correctly

[Mutablearray Addobject:null];

[mutabledictionary setobject:null forkey:@ "null"];

If we use nil, there is no run-time return warning, the runtime program crashes directly

[Mutablearray Addobject:nil];

[Mutabledictionary setobject:nil forkey:@ "nil"];

}

The above is nil, nil, NULL, nsnull difference, in the work we generally only use Nil, the other three we usually rarely use. But as a program of apes, we

This is a question that deserves our understanding.

[Swift, OC] Talk about the difference between nil, nil, NULL, Nsnull

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.