Wednesday, 26 October 2011

iPhone Development - Turn Off Automatic Reference Counting (ARC) on a Single File in a Project

Xcode has introduced ARC from version 4.2 and above (compatible with iOS 4.0 and above) and it is excellent. All new projects you create will have it enabled by default (you can uncheck this during the new project wizard if you so desire) and that is just lovely.

But what if you need to import legacy code, or another party's source code that hasn't been coded with ARC enabled? You need to remove all manual memory management commands such as release, retain, autorelease etc... if you want to enable ARC. So if your legacy or third party code has these in it and you can't or don't want to edit the code, then you'll have to tell Xcode that ARC is disabled for these files.

Thankfully, Xcode allows you to have ARC enabled for the whole project, but disabled on a select number of files.

To disable ARC for a single class, you should go to the "Project Navigator", click on your project, and then select the "Build Phases" tab. From the "Compile Sources" dropdown, find your class on which you wish to disable ARC. Note, you disable this on the .m implementation files, not the .h header files.

Notice that there are two columns here, "Name" and "Compiler Flags". Double click in the relevant "Compiler Flags" column for your desired class, and the following:


Clean and Rebuild your project to pick up these changes. Note, as far as I know, you cannot apply this to whole directories in one go, and have to apply it one file at a time.

For details on how to enable ARC for individual files, see my other post, Turn On ARC.