首页 > 移动平台 > 详细

iOS set方法

时间:2014-11-25 15:57:04      阅读:202      评论:0      收藏:0      [点我收藏+]

为了能让类的成员变量正确的被外接访问,我们需要设置set和get方法。

 

  1. @property (nonatomic,retain)NSString *test1;  
  2. @property (nonatomic,copy)NSString *test2;  


成员变量的属性不同(retain,copy)相对于的set方法也不同:

 

 

    1. @implementation SecondViewController  
    2. - (void)setTest1:(NSString *)test//retain  
    3. {  
    4.     // test需要先retain一次,放在自赋值时test被释放为nil  
    5.     [test retain];  
    6.     if (_test1 != nil) {  
    7.         [_test1 release];  
    8.     }  
    9.     //直接release无需判断也是可以的,iOS中对nil进行release操作合法  
    10.     //[_test1 release];  
    11.     _test1 = test;  
    12. }  
    13. - (void)setTest2:(NSString *)test//copy  
    14. {  
    15.     if (_test2 != nil) {  
    16.         [_test2 release];  
    17.     }  
    18.     // 也可以不用判断  
    19.     // [_test2 release];  
    20.     _test2 = [test copy];  
    21. }  

iOS set方法

原文:http://www.cnblogs.com/spiderdzl/p/4121015.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!