MRC下
重写setter
//属性声明
@property(nonatomic,strong) NSString *name;
//一旦重写了getter.setter方法,
//必须使用@synthesize variable = _variable来区分属性名与方法名
@synthesize name = _name;
//对应方法
-(void)setName:(NSString *)name
{
if (_name!=name)
{
[_name release];
_name = [name retain];
}
}
-(NSString*)name
{
return _name;
}
分别写出MRC环境下在assign、retain、copy下属性name对应的setter方法的内部实现.
//assign环境下
-(void)setName:(NSString *)name{
_name = name;
}
//retain环境下
-(void)setName:(NSString *)name{
if (_name != name) {
[_name release];
_name = [name retain];
}
}
//copy环境下
-(void)setName:(NSString *)name{
if (_name != name) {
[_name release];
_name = [name copy];
}
}
@interface People : NSObject
@property (nonatomic,retain) NSArray *oneArr;
@property (nonatomic,strong) NSArray *twoArr;
@end
@synthesize oneArr = _oneArr;
@synthesize twoArr = _twoArr;//同时写set get方法时要写
- (void)setOneArr:(NSArray *)oneArr{
if (_oneArr != oneArr) {
[oneArr release];
_oneArr = [oneArr retain];//或者copy视情况而定
}
}
- (NSArray *)oneArr{
if (!_oneArr) {
_oneArr = @[@"1",@"2",@"3"];
}
return [[_oneArr retain] autorelease];
}
- (void)setTwoArr:(NSArray *)twoArr{
if (_twoArr != twoArr) {
_twoArr = twoArr;
}
}
- (NSArray *)twoArr{
if (!_twoArr) {
self.twoArr = @[@"1",@"2",@"3"];
}
return _twoArr;
}