First, @property and @synthesize Basic specification usage
[Email protected]
When the compiler encounters @property, it is automatically expanded into getter and setter declarations
#import<Foundation/Foundation.h>@interfaceStudent:nsobject {int_age; int_no; float_height;}//when the compiler encounters @property, it is automatically expanded into getter and setter declarations@propertyintAge ;//-(void) Setage: (int) newage;//-(int) age;@propertyintNo;//-(void) Setno: (int) newno;//-(int) No;@propertyfloatheight;//-(void) SetHeight: (float) newheight;//-(float) height;- (void) test;@end
[Email protected]
@synthesize will automatically generate getter and setter implementations
#import "Student.h"@implementationStudent//@synthesize Age, Height, no;//@synthesize will automatically generate getter and setter implementations//@synthesize default to access variables with the same name as age//if a variable with the same name is not found, a private variable with the same name is generated automatically//@synthesize age;//age = _age represents getter and setter will go to access _age This member variable@synthesizeAge =_age;//-(void) Setage: (int) NewAge {//_age = newage;//}////-(int) Age {//return _age;//}@synthesizeHeight =_height;//-(void) SetHeight: (float) newheight {//_height = newheight;//}////-(float) Height {//return _height;//}@synthesizeNo =_no;//-(void) Setno: (int) Newno {//_no = Newno;//}////-(int) No {//return _no;//}- (void) Test {_age=Ten; _height=10.0f; _no=Ten; }@end
Second, @property and @synthesize advanced usage
Person.h
#import <Foundation/Foundation.h>@interface person:nsobject { int _id; float intfloat weight; @end
Person.m
#import "Person.h"@implementation Person@synthesizeID =_id;@synthesizeWeight =_weight;//-(void) Setweight: (float) Weight {//_weight = weight *;//}//-(float) Weight {//return _weight *;//}@end
Third, @property and @synthesize Ultimate usage
Teacher.h
#import <Foundation/Foundation.h>@interfaceint age ; @end
Teacher.m
#import " Teacher.h " @implementation Teacher // in the xcode4.5 environment, @synthesize can be omitted and the _age member variable is accessed by default // if the _age member variable is not found, a private member variable called _age is automatically generated -(void) test { ten;} @end
Third day of iOS (@property and @synthesize usage)