Attribute (@ property), @ synthesize, propertysynthesize
The previous instance variables we learned are as follows:
1 {2 int _age;3 int _height;4 int age;5 }
Learn attributes later
1 @property int age;
@ Property will automatically compile and generate the setter method and getter method declaration of a member variable.
1 - (void)setAge:(int) age;2 - (int)age;
Example:
@ Property int _ age;
Compile and generate
-(Void) set_age :( int) age;
-(Int) _ age;
That is to say, how do you compile and generate instance variables? In order to generate the declaration of the setter method and getter method of the standard instance variables, do not add _ to the attribute and set the setAge to uppercase.
Since @ property has the setter method of the instance variable and the declaration of the getter method, the implementation is @ synthesize property.
1 // write the attribute later. Specify the attribute to implement the setter and getter Methods 2 @ synthesize age.
We can see that @ synthesize will automatically compile and generate the setter and getter methods of a member variable.
1 - (void)setAge:(int)age {2 _age = age; 3 }4 5 - (int)age {6 return age; 7 }
If you want to access the member Variable _ age
@ Synthesize age = _ age;
In this way, the setter and getter methods of age are automatically generated, and the member Variable _ age is accessed.
Note: age on the left: the setter and getter methods of age must be implemented.
Age on the right: access the member variable "_ age" in the implementation. In this way, the attribute is associated with the instance variable.
Required member variables to be modified
The above @ synthesize age; if no member variable is specified, the system accesses the age by default.
In this case, the member variable "_ age" is automatically accessed. If the member variable "_ age" does not exist, the variable "@ private" is automatically generated. The variable "_ age" is private in the extension.
After XCode4.n @ property, the Declaration is generated and implemented.
1 @property int age;
Attribute is the description and implementation of a pair of setter and getter methods of the Instance Variable _ age.
By default, the setter and getter Methods access the instance variables starting _.
After learning this, we know why the member variables start with _ at the beginning. In fact, this is the case inside the system.