Objective-C语言自动参考计数
示例
使用自动引用计数(ARC),编译器会在需要的地方插入retain,release和autorelease语句,因此您不必自己编写它们。它还dealloc为您编写方法。
ARC的“手动内存管理”中的示例程序如下所示:
@interface MyObject : NSObject {
NSString *_property;
}
@end
@implementation MyObject
@synthesize property = _property;
- (id)initWithProperty:(NSString *)property {
if (self = [super init]) {
_property = property;
}
return self;
}
- (NSString *)property {
return property;
}
- (void)setProperty:(NSString *)property {
_property = property;
}
@endint main() {
MyObject *obj = [[MyObject alloc] init];
NSString *value = [[NSString alloc] initWithString:@"value"];
[obj setProperty:value];
[obj setProperty:@"value"];
}您仍然可以覆盖dealloc方法以清除ARC无法处理的资源。与使用手动内存管理时不同,您不会调用[superdealloc]。
-(void)dealloc {
//清理
}
热门推荐
10 财运虎年祝福语简短语
11 单亲爸爸春节祝福语简短
12 狗狗上厕所训练
13 植物种植祝福语简短
14 翻译考试祝福语朋友简短
15 个人周年庆简短祝福语
16 狗狗怎么训练
17 分别祝福语简短搞笑英文
18 生日广州男生祝福语简短