Objective-C Language Memory Management Automatic Reference Counting

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

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
}


Got any Objective-C Language Question?