On the difference between nil and null and nsnull and related issues

Source: Internet
Author: User

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, Nsnull,null and nil in essence should be the same, null and nil is actually 0, but in objective-c, for a type like nsarray, nil or null cannot be added to the object, If you define a Nsarray, allocate memory for it, and want to set the contents to be empty, you can initialize the contents of the nsarray with [NSNULL null], and I feel a bit like malloc a memory space in C. The value in this space is then initialized with Memset to 0.

[CPP] View plain copy

Print?

  1. _viewcontrollers = [[Nsmutablearray alloc] init];
  2. For (unsigned i = 0; i < _pages; i++) {
  3. [_viewcontrollers addobject:[nsnull Null]];
  4. }
  5. Ymbasecontroller *controller = [_viewcontrollers objectatindex:page];
  6. if ((NSNull *) controller = = [NSNull null])
  7. {
  8. ...
  9. }
  10. [_viewcontrollers replaceobjectatindex:page Withobject:controller];

3, 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.

On the difference between nil and null and nsnull and related issues

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.