1. @ Property
Only use the @ interface keyword in the. h file
When the compiler encounters @ property, it automatically expands the declaration of the getter and setter methods.
// Equivalent @ property int age;-(INT) age;-(void) setage :( INT) newage;
Note: When @ property is detected in xcode4.5, @ synthesize is automatically added to the. M file.
Age = _ age. For example, if the. h file does not declare the _ age variable, the private member Variable _ age is automatically added to the. M file.
2. @ synthesize
Only use the @ implements keyword in the. M file.
When the compiler encounters @ synthesize, it is automatically expanded into the implementation of the getter and setter methods. By default, the member variables with the same name are accessed.
@ Synthesize age = _ age; indicates that the get and Set Methods access the member Variable _ age.
// Equivalent @ synthesize age = _ age;-(void) setage :( INT) newage {_ age = newage;}-(INT) Age {return _ age ;}
NOTE: If no member variable is found, a private member variable with the same name is automatically generated.
3. Complete use
Person. h file
@interface Person:NSObject { int _ID; float _weight; } @property int ID; @property float weight; @end
Person. M file
@implement Person @synthesize ID = _ID; @synthesize weight = _weight; @end
4. Comprehensive use
The above @ property and @ synthesize keywords can be used in combination with the getter/setter method.
If the getter/setter method is manually implemented under certain special requirements, @ synthesize is invalid.
Person. h file
@interface Person:NSObject { int _ID; float _weight; } @property int ID; @property float weight; @end
Person. M file
@ Implement person-(void) setid: int newid {// In the getter/setter method, Special Operation _ id = newid * 100;}-(INT) id {return _ id;} @ end