转自:http://blog.csdn.net/hu_songsong/article/details/12982959
为了能让类的成员变量正确的被外接访问,我们需要设置set和get方法。
- @property (nonatomic,retain)NSString *test1;
- @property (nonatomic,copy)NSString *test2;
成员变量的属性不同(retain,copy)相对于的set方法也不同:
- @implementation SecondViewController
- - (void)setTest1:(NSString *)test//retain
- {
- // test需要先retain一次,放在自赋值时test被释放为nil
- [test retain];
- if (_test1 != nil) {
- [_test1 release];
- }
- //直接release无需判断也是可以的,iOS中对nil进行release操作合法
- //[_test1 release];
- _test1 = test;
- }
- - (void)setTest2:(NSString *)test//copy
- {
- if (_test2 != nil) {
- [_test2 release];
- }
- // 也可以不用判断
- // [_test2 release];
- _test2 = [test copy];
- }