The difference between ios-nil,nil,null

Source: Internet
Author: User

I. Brief description

1.nil is used to assign values to objects (any object in objective-c is of type ID)

2.NULL assigns any pointer, null and nil are not interchangeable

3.nil for Class pointer assignment (in OBJECTIVE-C the class is an object and is an instance of the class's Meta-class)

4.NSNull is used for collection operations

5. Although they represent null values, they are used in completely different situations.

Examples are as follows:

ID object = nil;    if (object) {        NSLog (@ "judgment object is not empty");    }        if (object = = nil) {        NSLog (@ "Judgment object is empty");    }        Nsarray *array = [[Nsarray alloc] initwithobjects:@ "First", @ "Second", nil];        NSString *element = [array objectatindex:2];    if ((NSNull *) Element)    {        NSLog (@ "Determines if the array element is empty");    }        Nsdictionary *dictionary = [[Nsdictionary alloc] initwithobjectsandkeys:@ "IPhone" @ "First" @ "IPad" @ "Second", nil];    NSString *value = [Dictionary objectforkey:@ "First"];    if ((NSNull *) value = = [NSNull null]) {        NSLog (@ "Determines whether the element of the Dictionary object is empty");    }

You are welcome to continue to add their differences.

two. DeepenNil,nil and Null differences: Nil is an object pointer is empty, nil is a class pointer is empty, NULL is the basic data type is empty.

1, Nil: general assignment to empty objects;

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

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. There is only one way to Nsnull:

+ (NSNull *) null;

three. Deepen again

On the difference between nil and null and its related problems

1, nil and null are simple to understand by literal means, nil is an object, and null is a value.

My understanding of nil is to set the object to NULL, and NULL is to set the base type to null, and the individual feels a bit like a property,

Basic type assignments are generally assigned copy for assign,nsstring types, whereas objects are generally retain.

And we're not going to generate crash or throw exceptions for the nil call method.

Look at some

Nil-Null-pointer to Objective-c object

NIL-Null-pointer to Objective-c class

null-> null pointer to primitive type or absence of data.

Take a look at the usage

Nsurl *url = nil;

Class class = Nil;

int *pointerint = NULL;

2, one can study the problem

In the Dealloc

-(void) dealloc

{

Self.test = nil;

[_test release];

Test = nil;

}

The difference between the few

First, the simplest [_test release]; This is to reduce the reference technology by 1, the so-called reference count is to see that there are multiple pointers to a piece of memory entity, when the release of the pointer is reduced by one, release to 0, it is really the memory of the return to the system of the time

Again self.test = nil; it's easy to understand the attributes and setter and Getter methods.

-(void) Settest: (NSString *) newstring

{

if (_test! = newstring)

[_test release];

_test = [newstring retain];

}

-(NSString *) test

{

return _test;

}

This is the setter and getter method, and in this problem the equivalent of the code changes to

if (_test! = nil)

[_test release];

_test = nil;

Now it is easier to explain that the setter method will retain the Nil object, before this has been the release of the old object, the advantage is that the member variable is not the opportunity to point to random data, and in other ways, there may be a situation pointing to random data. When the release is in place, if there is another way to access it, if it is already dealloc, it may be crash, and when it points to nil, no error will occur. Nil is the counter is 0, so to speak, when the real release of an object, NSLog is not print it points to the memory control, and when nil, it can be printed out to point to a memory space.

So now it's not difficult to explain test = nil; The simple use of this can be said to create their own memory leaks, it can be understood, is equivalent to the pointer to the object directly and the object clean break. The test points directly to nil, and the memory entities do not disappear, and there is no system reclamation.

The difference between ios-nil,nil,null

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.