OC内存管理的机制就是引用计数
自动引用计数(ARC)
在Build Settings中搜索 automatic 即可找到, 默认是打开的
手动引用计数(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则需要: