Use of iOS nil, null, and Nsnull

Source: Internet
Author: User

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.

Examples are as follows:

    1. ID object = nil;
    2. Judge object is not empty
    3. if (object) {
    4. }
    5. Null to determine Object
    6. if (object = = nil) {
    7. }
    8. Array initialization, NULL value end
    9. Nsarray *array = [[Nsarray alloc] initwithobjects:@ "First", @ "Second", nil];
    10. Determines whether an array element is empty
    11. NSString *element = [array objectatindex:2];
    12. if ((NSNull *) Element = = [NSNull null]) {
    13. }
Today when the project was encountered, to determine whether the array element is empty, I have the following notation, if (!element) if ([element length]>0) if (element== NULL) if (element = = Nil)
    1. Determines whether the element of a Dictionary object is empty
    2. Nsdictionary *dictionary = [Nsdictionary Dictionarywithobjectsandkeys:
    3. @ "IPhone" @ "First" @ "IPad" @ "Second", nil];
    4. NSString *value = [Dictionary objectforkey:@ "First"];
    5. if ((NSNull *) value = = [NSNull null]) {
    6. }
You are welcome to continue to add their differences. Deepen understanding of a

1, Nil: general assignment to empty objects;

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

Give me a chestnut (good weight ~):

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

+ (NSNull *) null;


nil是一个对象指针为空,Nil是一个类指针为空,NULL是基本数据类型为空。这些可以理解为nil,Nil, NULL的区别吧。

Deepen understanding of the second

On the difference between nil and null and its related problems

1, nil and null from the literal meaning to understand the simpler, nil is an object, and null is a value, I understand that nil is to set the object to NULL, and NULL is the basic type is set to empty, the personal feel a bit like the attribute, the basic type is assigned to assign NSString types are generally assigned copy, 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;

Nil is an object pointer is empty, nil is a class pointer is empty, NULL is the basic data type is empty. These can be understood as nil,nil, the difference between 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.

Ext.: http://blog.csdn.net/xdrt81y/article/details/8981133

Use of iOS nil, null, and 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.