Automatic Reference Counting

suggest change

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

- (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 can still override dealloc to clean up resources ARC does not handle. Unlike manual memory management, do not call [super dealloc].

-(void)dealloc {
   //clean up
}

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents