iOS talking about nil and nil differences and related issues

Source: Internet
Author: User
1, nil and null literal meaning is simpler, nil is an object, and null is a value, my understanding is nil is to set the object to NULL, and NULL is to set the basic type to empty, the individual feels 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 call the method on the nil without generating crash or throwing an exception.
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 null, nil is a class pointer is null, NULL is the base data type is empty. These can be understood as nil,nil, null differences.
2, one can study the problem
In the Dealloc
-(void) dealloc
{
Self.test = nil;
[_test release];
Test = nil;
}
The difference between these few
First of all, the simplest [_test release]; This is to reduce the reference technology by 1, the so-called reference count is to see how many pointers to a memory entity, when the release once, is the pointer reduced one, release to 0, is really the memory back to the system time

Besides self.test = nil; it's not hard 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 question the equivalent of just the code changed into
if (_test!= nil)
[_test release];
_test = nil;
It's easier to explain now that the setter method retain the Nil object, which has the advantage that the member variable does not even have the opportunity to point to the random data, and in other ways, it may point to random data. After release, if there is another way to access it, if it has been dealloc, may be crash, and point to nil, there will be no error. Nil plainly is the counter is 0, so to speak, when the real release of an object, NSLog is not able to 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 hard to explain test = nil; The simple use of this can be said to create their own memory leaks, here can be so understood, is equivalent to the pointer to the object directly and the object of the break. Let test directly point to nil, and the memory entity does not disappear and there is no system recycle.

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.