iOS Automatic Reference Counting (ARC)

iOS Automatic Reference Counting

  • Automatic Reference Counting (ARC) for Objective-C removes the programmer requirement to manage memory manually with retain and release
  • No need to call retain, release, autorelease anymore
  • Cannot explicitly invoke dealloc, or implement retain, release, retainCount or autorelease
  • if you implement dealloc, no need to use release to release objects and no need to [super dealloc] as it is done automatically
  • Use @autoreleasepool to mark the new autorelease block
  • Compiler will generate the reference counting code automatically

New property attributes when using ARC

@property(strong) MyClass *obj;
  • Same as below without ARC
    @property(retain) MyClass *obj;

Weak reference in Automatic Reference Counting (ARC)

@property(weak) MyClass *delegate;
  • Similar to below without ARC
    @property(assign) MyClass *delegate;
  • Except when the MyClass instance is deallocated, the variable delegate above will set to nil

To declare a weak reference variable

NSString __weak *string = ...