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 教师闺蜜祝福语简短