IOS member variable, instance variable, attribute variable difference, contact

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-specificstorage) An example of the English translation of Instance(manifestation of a class) is said to be "the performance of the classes", stating that instance variables should be variables defined by the class! 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 the @property (description 1, description 2, Description 3) (class *) VarName , there are 3 descriptive words to fill in (also 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 has a defined instance variable in {}Example:. h@property (automic,retain) nsstring * ABC;. M@sythesize ABC; 


//In the case of writing a @sythesize ABC, the system instance variables are not automatically generated "_ ABC   ", directly through the variable name ABC , that 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 "_")



//parentheses define the member variables (base data type + class-generated variable), the variables inside the. m file can be accessed directly into the parentheses by the variable name, self-> "variable name", but such an assignment can only be accessed by 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 either go directly to the variable name in the Self-> "{}" or use "{}" directly. To access the assignment, such a variable does not define the Setter,getter function and can only be assigned in the Assign way.






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.





IOS member variable, instance variable, attribute variable difference, contact


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.