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).
The relationship and difference between iOS instance variables and attribute variables