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. Select 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.

2) 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");
#else
    // ARC is Off
    NSLog(@"ARC is Off");    
#endif
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s