Ios-self. and underline access differences

Source: Internet
Author: User

This article reproduces the difference between self and underline from iOS .

1. The method was called through SELF.XXX Access: Contains the set and get methods. Access by underlining is to get its own instance variable, without the method of set and get.

2.self.xxx is access to a property, while _xxx is access to a local variable. All the members that are declared as attributes, and then ios5 need to use the compile instruction @synthesize to tell the compiler to help generate the properties of the getter and setter methods, after which the directive can be specified without regard, and by default the compiler will help us build.

The translator has a priority when generating the Getter,setter method, and he first looks for the Getter,setter method of whether the user defines the attribute in the current class, and if so, the compiler skips and no longer builds, using a user-defined method. This means that you are invoking a getter method when using Self.xxx. Will add one to the reference count, while the _XX

Does not use the reference count plus one. So using self.xxx is a better choice because it is compatible with lazy loading, and avoids the use of the slip line when the self is ignored, which easily causes circular references in the block. At the same time, using _ is not a property of the parent class, because it is only access to local variables.

Summary: The self method is actually called indirectly using the Get and set methods, and the underscore method is to manipulate the variables directly.



Ios-self. and underline access differences

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.