IOS self.使用

IOS self.使用 个人习惯;

只需要在属性初始化的时候使用self.属性,其他时候直接使用属性名就行;使用self.是 使retaincount+1,为了确保当前类对此属性具有拥有权

@interface CustomClass : UIViewController
{
    NSString *str
}
@property (retain, nonatomic) NSString *str;


@implementation CustomClass
@synthesize str;

-(void)viewDidLoad
{
    //方法一  用alloc必须手动释放一次
    self.str  =  [[NSString alloc]initWithString:@"my str"];
     [str release];
    //方法二 用类方法不用
   self.str =     [NSString stringWithString:@"my str"];

    以后调用时直接使用str,不必使用self.str
   [str appendString:@"\n"];
}

//在dealloc中必须释放
- (void)dealloc
{
    //方法一
    [str release];
    str = nil;
    //方法二
    self.str = nil;

    [super dealloc];
}        
原文地址:https://www.cnblogs.com/cocoajin/p/3081704.html