@synthesize用法
1)@property int age; @synthesize age;表示生成.h中变量age的get和set方法
注意;
如果@synthesize 变量名要先在.h文件中声明
@property int age;
.h-(void)setAge:(int)age;-(int)age;
.m-(void)setAge:(int)age{
slef->age = age;}
-(int)age{
returnage;}
@synthesize age;展开形式如下:
注意:
1) 错误用法,只写了@synthesize,没有写@property,也没有定义变量 NSString *name;2)@property类型xxx如果类中没有xxx这个实例变量,则会自动生成一个。
@synthesize指定实例变量赋值
1)@property int a;相当于下面的代码:
- (void)setA:(int)a{
_b=a;
}
- (int)a{
return _b;
}