如果将synthesize省略,语义特性声明为assign retain copy时,自己实现setter和getter方法

时间:2021-12-24 10:55:32

如果将synthesize省略,并且我们自己实现setter和getter方法时,系统就不会生成对应的setter和getter方法,还有实例变量

1,当把语义特性声明为assign时,setter和getter时方法内部实现

- (void)setName:(NSString *)name{

_name = name;

}

- (NSString *)name{

return _name;

}

2,当把语义特性声明为retain时,setter和getter方法内部实现

- (void)setName:(NSString *)name{

if (_name != name) {

[ _name release];

_name = [name retain];

}

}

- (NSString *)name{

return [[ _name retain] autorelease];

}

3,当把语义特性声明为copy时,setter和getter方法内部实现

- (void)setName:(NSString *)name{

if (_name != name) {

[ _name release];

_name = [name copy];

}

}

- (NSString *)name{

return [[ _name retain] autorelease];

}