Member variables, instance variables, attribute variables in iOS

Source: Internet
Author: User

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.

First, make a difference. instance variables, member variablesThe difference:

You can see that in the interface @interface parentheses are collectively referred to as "member variables", the instance variable is one of the member variables! The English translation of the instance variable is Instance Variable (object-specific storage) instance of the English translation of Instance (manifestation of a class) is said to be "class performance", stating that the instance variable should be by the class Define the variables! Remove basic data type int float .... And so on, other types of variables are called instance variables. * * Instance variable + basic data type variable = member Variable * *Now, let's talk. Properties:   in @property (description 1, description 2, Description 3) (class *) VarName, there are 3 descriptive words that need to be filled out (or you can not fill in the default values) 1. Nonatomic<-->atomic 2. Readwrite<-->readonly 3. Retain/copy/assign   let's start by introducing: retain : He refers to assigning a pointer to a memory area to a variable and adding 1 to the reference counter for that memory area. Each time the memory region's reference counter is incremented by 1, the memory area is freed when the reference counter of that zone becomes 0!   Copy : It refers to copying the value of the target memory area and then opening up a new memory area (new pointer) to paste the value. The variable is assigned a pointer to the new memory area!   Assign: It means that only the pointer to the target memory area is assigned to the variable, and the reference counter of that memory area does not change!   1, 22 points do not explain, 3 of retain, copy, assign all refers to, in the automatic generation of setter function, the compiler needs to identify a description of the word to generate corresponding setter function! It is important to note that if you do not add the class descriptor, the system defaults to the variable's setter method to take a assign approach. In the header file. h Typically there is an example of a defined instance variable in {}:. H@property (Automic,retain) nsstring * ABC;. M@sythesize ABC;

In the case of writing @sythesize ABC, the system does not automatically generate an instance variable "_ABC", directly through the variable name ABC, which is directly using the variable name in the assignment operation (= number to the left), just assign the memory area pointer to the variable, equivalent to Assgin. If you are assigning a value through the "point statement" self.abc=, it is necessary to see what kind of copy, retain, assign are defined in @property, and if you do not add the above description, the default is assign.

If you do not write @sythesize ABC, the system will automatically add an invisible member variable with "_" in the. h file {} By default (even if the variable name itself has "_")

The parentheses define the member variables (the base data type + class-generated variables), the variables in the. m file can be accessed directly into the parentheses by the variable name, self-> "variable name", but such assignment access can only be assign, The reference counter for the original object does not change.

[email protected] variable name; [email protected] Variable name =_ variable name; 3. Do not write @sythesize (refer to the variable names mentioned in the header file @property defined in the variable)

1. Member variable, the instance variable is directly accessed by the "variable name" or self-> "variable name", Assignment (assign). Self. Variable name implementation Setter,getter method.

2. Member variable, the instance variable is directly accessed by "_ Variable name" or self-> "_ Variable Name", Assignment (assign). Self. Variable name implementation Setter,getter method.

3. member variables. Instance variables (the system automatically adds "_" to the original variable name to generate the instance, member variable), directly through the self->_ variable name, or variable name directly to (assign). Self. Variable name implementation Setter,getter method.

If there are no variables defined through @property in the header file, but there are defined member variables in the {} and no @sythesize in the implementation file, you can access the assignment directly by self-> the variable name in {}, or by using the variable name in {} directly. Such variables do not define the Setter,getter function, only the assign way of assigning values.

Again to analyze the wording in @sythesize, @sythesize ABC directly in the. m file using SELF.ABC can call the member variable setter, getter function, directly call the member variable name ABC is to access the variable pointer, Assigning a value directly to a member variable is equivalent to the Assign effect.

Reference Link: http://www.cnblogs.com/AnnieBabygn/p/5335350.html

Member variables, instance variables, attribute variables in iOS

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.