Obj-C 2.0中属性的用法
假设一个接口如下
@interface TestInterface:NSObject {
NSArray *ary;
}
@property (nonatomic,retain) NSArray *ary;
-(void)InitAry;
@end
@implementation TestInterface
-(void)InitAry
{
ary = [NSArray arrayWithObjects:@"first",@"second",nil];
}
@end
上面这种写法是错误的。会导致ary值被autorelease掉。
一定要写成 self.ary = [NSArray arrayWithObjects:@"first",@"second",nil];
或[self setAry:[NSArray arrayWithObjects:@"first",@"second",nil]];
我想当然的以为这三种写法的效果是一样的。