The difference between self.abc = nil and [_ABC release] for iOS development

Source: Internet
Author: User

The former uses the property's point operator, which is equivalent to calling the ABC corresponding set method, and this sentence is the same: [Self setabc:nil];

The latter does not pass the property, directly accesses the member variable, and calls its release method.

For set method, using synthesize to let the system help us generate the set methods similar to the following:

-(void) SETABC: (ID) newabc{      if (_abc! = newabc) {           [_abc release];           _ABC = [newabc retain]; is retain or copy depends on your property declaration when attributes         }}

If the new value and the member are equal, there is no need to duplicate the assignment, if not equal, the new value needs to be assigned to the member, and the member _abc the original content is not required, here to call release to be released. (This specific reason is clearly written in the book of the Objective-c, please check).
So here, call self.abc = nil;
is tantamount to losing [_ABC release]; and _abc = nil;

SELF.ABC = nil; and [_ABC release]; Does not necessarily release the object, because the object may also be referenced by another, the intent of the operation here is: Other places with useless _abc I do not know, here _abc I do not need.

When using retain or copy of the property attributes, Self.abc = nil, equal to the following statement:
if (_abc!= nil)
{
[_ABC release];
_ABC = nil; Message sent to nil returns nil.
}
So it's a little easier to use.

The difference between self.abc = nil and [_ABC release] for iOS development

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.