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 送给学长毕业祝福语简短