Objective-c language--attributes and instance variables

Source: Internet
Author: User

This article turns from http://blog.csdn.net/addychen/article/details/39525681

Use objective-c for some time, has not been clear in the Objective-c attribute and instance variables, here summarizes, objective-c in the first instance variable, needs to give the external class uses the @public declaration, Internal use of @private or @protect statements. Objective-c I added attributes, I think Apple's consideration should be that attributes are used externally and the strength variables are primarily used within the program. This facilitates the separation of the code, because the compiler provides the corresponding instance variable for the property directly (you can also specify the instance variable for that property manually) and the Getter/setter method.

Property in class

In the old version of the Objective-c language, we need to declare both the attribute and the underlying instance variable at that time, the property is a new mechanism for the Objective-c language and requires that you declare the instance variable that corresponds to it, for example:

[OBJC]View Plaincopyprint? < param name= "wmode" value= "Transparent" >
    1. @interface myviewcontroller:uiviewcontroller{
    2. __strong UIButton *_mybutton;
    3. }
    4. @property (nonatomic, retain) UIButton *mybutton;
    5. @end

Later, Apple converted the default compiler from GCC to LLVM (low-level virtual machine), where it was no longer necessary to declare instance variables for a property. If LLVM discovers a property that does not have a matching instance variable, it automatically creates an instance variable that begins with the underscore. Therefore, in this version, we no longer declare instance variables for the export output.

Example: MyViewController.h file

[OBJC]View Plaincopyprint?
    1. @interface Myviewcontroller:uiviewcontroller
    2. @property (nonatomic, retain) UIButton *mybutton;
    3. @end

In the myviewcontroller.m file, the compiler will also automatically generate an instance variable, _mybutton. The _mybutton instance variable can be used directly in the. m file, or through the property Self.mybutton. It's all the same.

Note that the Self.mybutton here is actually the Getter/setter method that invokes the MyButton property. This is different from the use of midpoint in C + +, where points in C + + can directly access member variables (that is, instance variables).

For example, in Objective-c, there is the following code

. h file

[OBJC]View Plaincopyprint? < param name= "wmode" value= "Transparent" >
    1. @interface myviewcontroller:uiviewcontroller{
    2. __strong NSString *_name;
    3. }
    4. @end

An expression such as self.name is wrong in a. m file. Xcode will prompt you to use-> change it to Self->name. Because the OC midpoint expression represents the calling method, the above code does not have the name method.

OC syntax Description of the point expression: "dot expression (.) It looks a bit like the access to the struct in C and the Java language Summary of object access, which is intended by OC designers. If a point expression appears on the equals sign = left, the setter method for the property name is called. If the point expression appears on the right, the getter method for the property name is called. ”

So the expression in OC is actually a shortcut to the setter and getter method of the calling object, for example: Dealie.blah = greeble is equivalent to [Dealie.blah setblah:greeble];

In the previous usage, declare the attribute with the instance variable that corresponds to it:

[OBJC]View Plaincopyprint?
    1. @interface myviewcontroller:uiviewcontrolle{
    2. __strong UIButton *_mybutton;
    3. }
    4. @property (nonatomic, retain) UIButton *mybutton;
    5. @end

This approach is mostly used, and most of it is now in use, since many open source code is this way. However, after the iOS5 update, Apple is recommended to use the following methods:

[OBJC]View Plaincopyprint? < param name= "wmode" value= "Transparent" >
    1. @interface Myviewcontroller:uiviewcontroller
    2. @property (nonatomic, retain) UIButton *mybutton;
    3. @end

Because the compiler automatically generates an instance variable _mybutton that starts with an underscore, you do not have to manually write the instance variable yourself. And there is no need to write @synthesize MyButton in the. m file; The Setter,getter method is automatically generated for you. @synthesize's role is to have the compiler automatically generate setter and getter methods for you.

@synthesize also has a role, you can specify an instance variable corresponding to the property, such as @synthesize MyButton = xxx; then Self.mybutton is actually the instance variable xxx of the operation, not _mybutton.

In the actual project, we generally write this. m file

@synthesize MyButton;

Once this is written, the compiler automatically generates MyButton instance variables, along with the corresponding getter and setter methods. Note: _mybutton This instance variable is not present, because the automatically generated instance variable is MyButton instead of _mybutton, so now the @synthesize function is equivalent to specifying the instance variable;

If @synthesize MyButton is written in the. m file, then the generated instance variable is MyButton, and if @synthesize MyButton is not written, then the generated instance variable is _mybutton. So there is a slight difference from the previous usage.

(GO) objective-c language--attributes and instance variables

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.