美文网首页
引用计数

引用计数

作者: Dove_Q | 来源:发表于2016-09-19 15:34 被阅读8次

OC内存管理的机制就是引用计数

自动引用计数(ARC)

在Build Settings中搜索 automatic 即可找到, 默认是打开的


Snip20160921_3.png

手动引用计数(MRC)

CustomView *cv = [CustomView new];    
    CustomView *cv2 = cv;
    [cv2 retain];
    [cv release];
    cv2.backgroundColor = [UIColor redColor];
                          
    [cv2 release];

    [self.view addSubview:cv];
    [cv release];

只读变量

@interface Animal : NSObject
//只能读不能赋值
@property (nonatomic,copy,readonly)NSString 
*DNA;
@end

@implementation Animal
- (instancetype)init{
    self = [super init];
    if (self) {   
        _DNA = @"lol";
    }
    return self;
}
@end

注意: 若添加的第三方框架不支持ARC则需要:


Snip20160921_4.png

相关文章

网友评论

      本文标题:引用计数

      本文链接:https://www.haomeiwen.com/subject/kahnettx.html