Links and differences between the "intercept" IOS member variables, instance variables, and attribute variables

Source: Internet
Author: User

Attribute property in class

In the first version of iOS:

We declare both the property and the underlying instance variable for the output, when the property is a new mechanism for the OC language, and you must declare the instance variable that corresponds to it, for example:

Note: (This is the previous usage)

@interface myviewcontroller:uiviewcontroller{    UIButton *mybutton;} @property (nonatomic, retain) UIButton *mybutton; @end

In the current iOS version:

Apple converts the default compiler from GCC to LLVM (Low level virtual machine), and it is no longer necessary to declare instance variables for attributes. 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.

After the ios5 update, Apple is recommended to use the following methods:

@interface Myviewcontroller:uiviewcontroller@property (nonatomic, retain) UIButton *mybutton; @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, and the Setter,getter method is automatically generated for you.

@synthesize role: (1) Let the compiler automatically generate setter and getter methods for you. (2) You can specify an instance variable corresponding to the attribute, such as @synthesize MyButton = xxx, then Self.mybutton is actually the instance variable xxx of the operation, not _mybutton.

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

Two, instance variable and attribute variable use method

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. It is wrong to use Self.yourbutton to access the Yourbutton variable, and Xcode will prompt you to use->, and change it to Self->yourbutton. Because the expression at the midpoint of the OC represents the call to the Yourbutton method, the code above does not have a Yourbutton method, and the Yourbutton can be used directly.

Third, attribute property in the category

Class is distinguished from the attributes that are added to the category, because only methods can be added to the category, and instance variables cannot be added. It is often seen in iOS code to add attributes to a category, in which case variables are not generated automatically. For example, the Uiviewcontroller class is extended in the UINavigationController.h file.

@interface Uiviewcontroller (Uinavigationcontrolleritem) @property (nonatomic,readonly,retain) Uinavigationitem * Navigationitem; @property (nonatomic) BOOL hidesbottombarwhenpushed; @property (Nonatomic,readonly,retain) Uinavigationcontroller *navigationcontroller; @end

The attributes added here do not automatically generate instance variables, and the attributes added here are actually added getter and setter methods.

Note that anonymous categories (anonymous extensions) can add instance variables, and non-anonymous categories cannot add instance variables, only methods, or properties (which are actually methods).

Relationship of member variables, instance variables and property variables

@interface Myviewcontroller:uiviewcontrolle{uibutton *yourbutton;int count;id data;} @property (Nonatomic, Strong) UIButton *mybutton; @end

The variables declared in {} are member variables. So Yourbutton, count, and data are all member variables. So what does an instance variable mean?

An instance variable is essentially a member variable, but an instance is a declaration of a class for a class. The Yourbutton in {} is the instance variable. ID is an OC-specific class, essentially an ID equivalent to (void *). So ID data belongs to the instance variable.

Member variables are used within a class, without the need for a variable to be exposed to outside. Because the member variable does not generate a set, get method, the outside world cannot touch the member variable. depending on the private nature of the member variable , the attribute variable is available for easy access. The benefit of a property variable is that it allows other objects to access the variable (because the set and get methods are automatically generated during property creation). Of course, you can set up read-only or writable, and the settings can be customized. Therefore, a property variable is a variable that is used to interact with other objects.

In summary, the member variable is a variable defined in the {} number, and if the variable's data type is a class, the variable is called an instance variable. Because instance variables are a special case of member variables, instance variables are also used internally within the class, without the need for external contact variables, which is known as class-private variables. Property variables are variables that are used to interact with other objects.

However, it seems that people do not like to use member variables to define class variables, like to use attribute variables to define the class variables. Define the variables that need to be exposed to external contact in the. h file, only the variables used in this class are defined in the. m file.

Links and differences between the "intercept" IOS member variables, instance variables, and attribute variables

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.