Analysis on the usage and relationship of the attributes and member variables of OBJECTIVE-C

Source: Internet
Author: User



After a period of iOS development using the Objective-c language, it was found that its language base was relatively weak and began to compensate for its shortcomings. I found that after using a language, many of the principles behind it would find a deeper understanding. The following is the confusion of my properties and member variables of the use and relationship problems, because the level of limited may be wrong, please read the article to correct.



1. Properties



The use of attributes in Apple's official document "The OBJECTIVE-C programming Language" in the detailed description, here will not repeat, the link is as follows:



"The Objective-c programming Language"



If your English is not good, it does not matter, has been all translated this document, the link is as follows:



Objective-c Official document translation for programming languages



2, about @synthesize object = _object Explanation



We'll see this in a lot of code:



[Plain] view plaincopy


  1. @interface myclass:nsobject{
  2. Myobjecct *_object;

  3. }

  4. @property (nonamtic, retain) Myobjecct *object;

  5. @end

  6. @implementatin MyClass

  7. @synthesize Object=_object;


I looked up some of the blogs I wrote with other people on the internet and summed up several reasons for this:



(1) Differences between 32-bit systems and 64-bit systems



In a 32-bit system, if the @interface part of the class does not have a Ivar (instance variable) declaration, but there is @property declaration, there is a @implementation in the @synthesize part of the class, You will get a compilation error similar to the following:



Synthesize property ' XX ' must either is named the same as a compatible Ivar or must explicitly name an Ivar



On a 64-bit system, the runtime automatically adds Ivar to the class, and the added Ivar is prefixed with an underscore "_".



(2) Avoid puzzling bugs



Here's a brief look at the difference between _object and object. _object is a member variable of the MyClass class, and object is a property. Property and synthesize define a pair of getter and setter methods, where the Getter method is the Object,setter method is setobject, in fact the getter and setter methods manipulate the variable _object.



If you write synthesize objec = _object, The Getter method is:



[Plain] view plaincopy


    1. -(MyObject *) object

    2. {

    3. return _object;

    4. }


If you write synthesize object, the Getter method is:



[Plain] view plaincopy


    1. -(MyObject *) object

    2. {

    3. return object;

    4. }


Unexpected errors occur when the name of the function and the name of the attribute are the same, and in order to avoid this bug,apple, most of the demo code is used in this way.



(3) Use of attributes and variables



The Self.object property is invoked using the Getter method, which can be used outside the class. The variables are called through _object and can only be used in the corresponding implementation of the class and cannot be used outside the class.



Let's look at two assignment actions:






[Plain] view plaincopy


    1. Self.object = [[MyObject alloc] init];

    2. Object = [[MyObject alloc] init];


The first approach relates to @property (Nonamtic,retain), which is actually assigned by invoking the setter method SetObject. The second way is a simple pointer assignment, without invoking the setter method.



Here are the changes in Retaincount:



[Plain] view plaincopy


    1. MyObject *tmp = [[MyObject alloc] init];

    2. Self.object = tmp; Retaincount = 2

    3. [TMP release]; Retaincount = 1

    4. _object = [[MyObject alloc] init]; Retaincount = 1





Analysis on the usage and relationship of the attributes and member variables of OBJECTIVE-C


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.