January 10, 2014

Mixing ARC with Non-ARC in iOS

Apple announced support for ARC (Automatic Reference Counting) from iOS 5.

Prior to iOS 5, (i.e. Non-ARC) developer had to keep track of each object allocation manually and release after that object was not needed. Non-ARC also know as MRC (Manual Reference Counting).

ARC is not a runtime mechanism rather, it is a compiler level feature that automatically inserts release/retain statement during the compilation.

Developer can mixed up ARC and MRC files in a project as well as code snippet in a single source file.

  1. Exclude Non-ARC file from being compiled with ARC

    You can do that by setting the flag on the .m file:

    Steps :~
  1. Click the Project
  2. Target
  3. Build Phases Tab
  4. Compile Sources Section
  5. Double Click on the file name and add -fno-objc-arc to the popup window.

Note: If you want to include a file in ARC, you can use the -fobjc-arc flag.

  1. Mixing ARC with Non-ARC code in a source file.

    You can use __has_feature, like this:

    #if __has_feature(objc_arc)
       // ARC is On
       NSLog(@"ARC is On");
       // ARC is Off
       NSLog(@"ARC is Off");