Posted on by & filed under Content - Highlights and Reviews, Mobile Development.

To many programmers, developing for iOS felt like a step backward due to the need to carefully manage memory with retains and releases. That all changed with iOS5—or more specifically with the LLVM 3.0 compiler, which is bundled with IOS5.

ARC stands for Automatic Reference Counting.  Basically, you convert your code to ARC and afterward never need to manage memory again. Instead, the compiler will automatically retain objects as long as something points to them. What could be easier? (And, more accurate; it’s wonderful to no longer have to track down mysterious memory problems created by coding mistakes.)

Using ARC for New Projects

Starting a brand-new project with ARC is very easy. When you create a project, you’ll see an option that says “Use Automatic Reference Counting”. It’ll be checked by default. Leave it checked.

That’s really all there is to it. Retains and releases will automatically be implemented by the compiler, but you should never have to worry about them. All you do have to worry about is maintaining references to any variables that you want to keep around.

A reference can be something as simple as this:

There’s nothing more to it.

Using ARC for Old Projects

Converting an existing project to ARC is almost as easy. You just need to run “Edit > Refactor > Convert to Objective-C ARC …”.  You then select the target to convert to LLVM 3.0 and hit the “precheck” button.

At this point, you might have some problems, as shown by this pop-up dialogue:

If you see this error (and you probably will), you need to make the change to “Xcode > Preferences” that it suggests. Then, Refactor (again).

The Issues Manager should then show you what’s going wrong. Look for“Automatic Reference Counting Issues” or errors specifically related to “retain”, “release”, or “autorelease”.

In this particular case, some variables hadn’t been cast, so the compiler didn’t recognize a method, and an autorelease had been used incorrectly in the project … but the actual problems will vary depending on your individual code.

Once you work through any problems, your code will be converted to ARC, and you’ll never have to worry about memory management again.

Safari Books Online has the content you need

Try out some of the great iOS 5 books available in Safari Books Online:

From the basic building blocks to including drawing, responding to user interaction, animation, and sound, Beginning iOS Game Development provides a one-stop-shop for getting your iOS game up and running.
Erica Sadun has thoroughly revised The iOS 5 Developer’s Cookbook: Core Concepts and Essential Recipes for iOS Programmers, Third Edition to focus on powerful new iOS 5 features, the latest version of Objective-C, and the Xcode 4 development tools.
Objective-C is the universal language of iPhone, iPad, and Mac apps, and Objective-C for Absolute Beginners: iPhone, iPad and Mac Programming Made Easy, Second Edition starts you on the path to mastering this language and its latest release.

About the Author

  Shannon Appelcline is a versatile author and programmer. He currently works as the lead iOS developer for Skotos Tech, an online entertainment company. In the past two years, he’s written five iOS games for them, all based on tabletop releases by popular German designers. The first of these was Reiner Knizia’s Money (2010)—which has also been ported to MacOS—while the most recent was Reiner Knizia’s Modern Art: The Card Game (2011). Shannon’s two most recent books show the breadth of his interests. They are iOS4 in Action (2011), published by Manning Publications, and Designers & Dragons: A History of the Roleplaying Game Industry (2011), published by Mongoose Publishing.

Tags: Automatic Reference Counting, iOS 5, Xcode,


  1.  iOS 5 Tip: Use Storyboard for Layout Design « Safari Books Online's Official Blog