With automatic reference counting (ARC), the compiler inserts retain
, release
, and autorelease
statements where they are needed, so you don't have to write them yourself. It also writes dealloc
methods for you.
The sample program from Manual Memory Management looks like this with 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;
}
@end
int main() {
MyObject *obj = [[MyObject alloc] init];
NSString *value = [[NSString alloc] initWithString:@"value"];
[obj setProperty:value];
[obj setProperty:@"value"];
}
You are still able to override the dealloc method to clean up resources not handled by ARC. Unlike when using manual memory management you do not call [super dealloc]
.
-(void)dealloc {
//clean up
}