Differences between SELF.XXX and _xxx underline in iOS

Source: Internet
Author: User

Property (nonatomic,copy) NSString *propertyname; Self.propertyname is a call to a property, and _propertyname is a call to a partial variable. In its 1, @property declaration, the compiler is born G]


@property (nonatomic,copy) NSString *propertyname;

Self.propertyname is the access to the attribute;

_propertyname is access to local variables.

In one, @property declaration, the compiler has a priority when generating the Getter,setter method, first locating the Getter,setter method of whether the user has defined the property in the current class, and, if so, the compiler skips and no longer builds, using a user-defined method.

This means that you are calling a getter method when you are using Self.propertyname.

Self.propertyname will let the counter +1;

Second, _propertyname, is the direct call variable, not through Getter method.

_propertyname is similar to Self->_propertyname.

Using Self.propertyname is a better choice because it is compatible with lazy loading and avoids the use of underscores when ignoring the self, which makes it easy to create circular references in blocks.

At the same time, using _ is not a property of the parent class, because it is only access to local variables.

Finally, the self method is actually called indirectly by the Get and set methods, and the underscore method is to manipulate the variables directly.

Differences between SELF.XXX and _xxx underline in iOS

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.