Reprinted from Http://www.cnblogs.com/crazypebble/p/3439261.html
Attribute property in class
In the first version of iOS, we declare both the attribute and the underlying instance variable for the output, at which point the attribute is a new mechanism for the OC language and requires that you declare the instance variable that corresponds to it, for example:
@interface myviewcontroller:uiviewcontroller{ UIButton *mybutton;} @property (nonatomic, retain) UIButton *mybutton; @end
More recently, Apple has converted the default compiler from GCC to LLVM, which is no longer required 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
@interface Myviewcontroller:uiviewcontroller@property (nonatomic, retain) UIButton *mybutton; @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 OC there is the following code
. h file
@interface myviewcontroller:uiviewcontroller{ nsstring *name;} @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:
@interface myviewcontroller:uiviewcontrolle{ UIButton *mybutton;} @property (nonatomic, retain) UIButton *mybutton; @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:
@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; 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.
Ii. attribute property in a 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 difference between a property and a member variable in the "Go" ios