You are on page 1of 10

Automatic Reference Counting

What is ARC?
Not a GC. A compiler level feature. No retain/release/autorelease required.

Enable/Disable ARC
When creating project. In build options On individual files with compiler options (-fobjc-arc, -fno-objc-arc).

Limitations
Only on ObjC objects, not on CF, malloc, free. Compiler LLVM 3.0 or greater. Zeroing Weak Reference only on ios 5.0 and above, else 4.0 and above.

New Keywords/Lifetime Qualifiers introduced


Property Attributes
strong, weak, unsafe_unretained

Variable Qualifiers
__strong __weak __unsafe_unretained __autoreleasing

@autoreleasepool Bridging
__bridge __bridge_transfer __bridge_retained

Properties
strong ~ retain Default is strong weak ~ assign with zeroing weak reference. unsafe_unretained ~ assign.

@property(strong) NSString *myString; @property(weak) IBOutlet UIView *view; @property(unsafe_unretained) int iVar;

Variables
__strong Default is __strong __weak is with zeroing weak reference. __unsafe_unretained.

__strong NSString *yourString = @"Your String"; __weak NSString *myString = yourString; yourString = nil; __unsafe_unretained NSString *theirString = myString; /* All pointers will be nil at this time */

@autoreleasepool
Previously
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init]; int retVal = UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); [pool release]; return retVal; Now

@autoreleasepool { int retVal = UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); return retVal; }

Bridging
__bridge transfers a pointer between Objective-C and Core Foundation with no transfer of ownership. __bridge_retained or CFBridgingRetain casts an Objective-C pointer to a Core Foundation pointer and also transfers ownership to you. You are responsible for calling CFRelease or a related function to relinquish ownership of the object. __bridge_transfer or CFBridgingRelease moves a nonObjective-C pointer to Objective-C and also transfers ownership to ARC. ARC is responsible for relinquishing ownership of the object.

New Rules
No [super dealloc], retain, release, retainCount, or autorelease allowed. Cannot use object pointers in C structures, use Obj-C class instead. You can use __unsafe_unretained though. You cannot give an accessor a name that begins with new, @property (strong, nonatomic, getter=theNewTitle) NSString *newTitle; Have to use Toll Free Bridging for typecasting to/from Obj-c object.

You might also like