iOS class implementation How to use attributes

Source: Internet
Author: User

Properties (properity) is a very useful thing, simple and straightforward. Objective-c also created a point syntax to help you use it. According to previous C + + habits, when accessing instance variables outside of the class, it is best to pass the Getter/setter method, that is, through the property to do, then the class internal implementation?

We must be clear that the C language programming philosophy is "what the programmer writes, is what he wants", this is the design idea of C, deep bone marrow, of course, also in the objective-c inherited down.

So before we answer the question of which method to implement within the class, we need to know the difference between the two methods in detail. There are many objective-c in the introductory book will intentionally or unintentionally instilled "there is no difference between the two, just the compiler to help you save a little implementation of the Code" concept, I have to say, this is too rough, for an int type variable is true, but for an object, the situation is complex, Is the object retain or copy? For direct use of instance variables, no explicit behavior is given-this means that the object simply points to a new value, neither releasing the old object nor retain the new object, which often leads to memory problems. In addition, if such a direct assignment, it does not trigger KVO.

However, direct use of instance variables is better than the use of attributes, so in the implementation of the class is the king, that is to say: Read the instance variable directly, do not use the attribute point syntax; The write instance variable uses the attribute's point syntax. This avoids the memory problem, kvo the problem, and improves the efficiency as much as possible.

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.