The topic of this post is one that I’m sure many developers will know about especially since it is a footnote in several of the tutorials on the ARC tutorial and guide page on this site.
I’ve received so many inquiries about how to get open source libraries mentioned on this site and it’s surprising how many don’t know how to implement the –fno–objc–arc flag for using non-ARC enabled code within ARC enabled projects.
If you don’t know how to do this working with many of the open source projects on this site can be a living hell – so here’s the easy way to get nearly all of those Non-ARC enabled libraries working within your ARC enabled projects.
So here are two scenario’s you will likely run into when mixing ARC enabled and non ARC enabled code and how to solve them easily in Xcode 4.
Scenario 1 – You have non-ARC project that you want to refactor to enable ARC (using XCode’s refactoring feature), but there is existing code that you do not want to convert.
Here’s how to solve this, when you do the refactoring deselect the files that you don’t want to convert to arc, and Xcode will automatically add the -fno-objc-arc flag to those files.
1. Choose Edit->Refactor->Convert to Objective-C ARC in the Xcode menu.
2. On the Select Targets To Convert screen select your targets, then click the little arrow beside the target name to expand the list of files.
3. Highlight the files that you do not want to convert then right click (or ctrl-click) and select uncheck-all to deselect all the files you want to convert to ARC. Then proceed as usual with the refactoring.
You can see this in the screenshot below where this has already been done with Cocos2D (btw, this is the easy way to get the Cocos2D template projects working with ARC):
Scenario 2 – You already have a project using ARC and want to add in some non-ARC enabled code, or you want to refactor the project yourself and don’t want to use ARC with some of the code.
I’ve seen this come up all over the internet in answers, and people asking where to add the -fno-objc-arc flag so here’s how it’s done.
1. Bring up the build phases tab for the appropriate target.arc1
2. Change the compiler flags for the specific code files that you do not want to enable ARC for to -fno-obj-arc (simply clicking on the compiler flags section beside the file, and enter -fno-objc-arc).
The flag will show up beside the file like in this screenshot if done correctly:
Continue as normal – the compiler now knows not to use ARC with the files that you flagged.
I can’t verify that it works in every case as there are some libraries with things such as embedded projects that can mess things up, but in my opinion it’s good to try the easy way first – and I haven’t seen it not work with any of the newer open source libraries I’ve mentioned on this site in the last few months.
You might want to look at this template that allows you to create universal frameworks easily if you run into issues with the -fno-objc-arc flag and link to those projects as frameworks.
Submit A Resource
Have you created a useful tutorial, library or tool for iOS development that you would like to get in front of our 300,000+ monthly page views from iOS developers?
You can submit the url here.
The resources we feel will appeal to our readers the most will be posted on the front page.