使用自动引用计数(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无法处理的资源。与使用手动内存管理时不同,您不会调用[super dealloc]。
-(void)dealloc {
//清理
}