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則需要: