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.
For example: MyViewController.h File
@interface Myviewcontroller:uiviewcontroller@property (nonatomic, retain) UIButton *mybutton; @end
.
note
For example, in OC there is the following code
. h file
@interface myviewcontroller:uiviewcontroller{nsstring *name;} @end
self.name is wrong. Xcode will prompt you to use-> change it to Self->name.
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.
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 completely 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
@synthesize's role is to have the compiler automatically generate setter and getter methods for you.
also has a function to specify an instance variable corresponding to the property, < span> So 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;
> Note: _mybutton This instance variable does not exist because the automatically generated instance variable is MyButton instead of _mybutton, So now the @synthesize function is equivalent to specifying instance variables;
if @synthesize MyButton is written in the. m file, then the generated instance variable is MyButton; If you do not write @synthesize MyButton, then the generated instance variable is _mybutton. So there is a slight difference from the previous usage.
Ii. Property properties in category
. For example: 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).
From:http://www.cnblogs.com/crazypebble/archive/2013/11/23/3439261.html
"Reprint from and Organize http://blog.csdn.net/itianyi/article/details/8618128"
Some suggestions:
1. If it is purely a private variable, it is best to declare it in implementation.
2. If the public attribute of the class is used, the property is written in the. h file
Span style= "margin:0px; padding:0px; line-height:1.8; " >3. If you need a setter and getter internally to implement something, declare
in the class of the. m file.
In From:oc, what are the differences between local variables, attribute variables and member variables, and how are they related to each other?
An instance variable between the curly braces {} in the. h interface, which can be used directly in. m;
The property variable in. h, which requires the use of the propertyvariable variable in the. M self.propertyvariable method
Differences between attributes and member variables in iOS