How To Enable ARC in a Cocos2D 2.X Project

This is a post by Tutorial Team Member Tony Dahbura, an independent iOS developer with FullMoon Manor LLC. You can also find him on Google+. Automatic Reference Counting (or ARC for short) makes memory management in your apps much easier. ARC’s major benefit is freeing you from worrying about whether you cleaned up that sprite […] By Tony Dahbura.

Leave a rating/review
Save for later
Share
You are currently viewing page 3 of 3 of this article. Click here to view the first page.

Enabling ARC For Your New Game Using Xcode

Are you back? OK, time to get to work again. :] In the last section, you took the old Cocos2D 1.x version of PewPewNinjas and converted it to run using Cocos2D 2.x. But the project is still old-skool: it isn’t ARC-compliant. Time to change that!

Open your project (if it isn’t already) and from the Xcode menu, select Edit\Refactor\Convert to Objective-C ARC.

Make sure the checkbox at the top is unchecked.

Select the drop-down arrow next to PewPewNinjas.app so that it expands to show all the source files in the project. Scroll to the bottom of the list of files and select main.m, AppDelegate.m, IntroLayer.m, GameOverScene.m, and HelloWorldScene.m.

This dialog asks for files that should be checked and corrected to be ARC-compatible. Since the Cocos2D core is not ARC-compliant, there should not be any files checked other than the five mentioned above. Click the Check button. You will be greeted by a dialog box asking you to confirm the changes.

Click Next.

After a brief period of analysis, you will see the five files you selected, plus some additional .h files that also need fixing. There’ll also be a split view where the left pane shows the needed changes, and the right pane shows the original source.

Click on AppDelegate.m and note the analyzer changes.

Look through each of the files so you can get a feel for the code changes made by Xcode.

Note that in AppDelegate.m, the analyzer completely removed the dealloc method. This is different from what you did earlier, where you removed the release calls but left the method intact. Of course, since there is no code in dealloc, it makes sense to remove the method completely.

This is a great time to learn about some of the differences between ARC and the traditional model of memory management. You will notice some of the following:

  • The retain keyword is replaced by strong.
  • All release and autorelease statements are removed.
  • [super dealloc] is removed.
  • The dealloc method is altogether removed where it isn’t needed/used.

Since you have no issues with the changes suggested by Xcode, click the Save button. You will be prompted as to whether Xcode should create a snapshot of your project before making all these changes. In this case, since it is a conversion from the base template, just select Disable.

You will now be returned to your project with the necessary changes in place for ARC. It’s as simple as that!

Summary of Changes Xcode Performed

Here’s a quick reference to what the wizard did for you:

In AppDelegate.h:

CCDirectorIOS	*__unsafe_unretained director_;
@property (nonatomic, strong) UIWindow *window;
@property (unsafe_unretained, readonly) CCDirectorIOS *director;

In AppDelegate.m:

  • Removed method – (void) dealloc

In GameOverScene.h:

@property (nonatomic, strong) CCLabelTTF *label;
@property (nonatomic, strong) GameOverLayer *layer;

In GameOverScene.m:

  • Removed two – (void) dealloc methods
[[CCDirector sharedDirector] replaceScene:[[HelloWorldScene alloc] init]];

In HelloWorldScene.h:

@property (nonatomic, strong) HelloWorld *layer;

In HelloWorldScene.m:

  • Removed method – (void) dealloc under HelloWorldScene implementation
  • Removed [targetsToDelete release];
  • Removed [projectilesToDelete release];
  • Modified method – (void) dealloc under HelloWorld implementation
- (void) dealloc
{
	// in case you have something to dealloc, do it in this method
	// in this particular example nothing needs to be released.
	// Cocos2D will automatically release all the children (Label)
	_targets = nil;
	_projectiles = nil;
	
	// don't forget to call "super dealloc"
}

In main.m:

  • Modified method int main
int main(int argc, char *argv[]) {
    
    @autoreleasepool {
      int retVal = UIApplicationMain(argc, argv, nil, @"AppController");
      return retVal;
    }
}

Look around and check out the Build Settings for your project – you will see that the ARC flag has been set to YES.

Now build and run one final time. :]

Enjoy your new Cocos2D v2.x game utilizing ARC!

Where To Go From Here?

Here is the complete code for PewPewNinjas with the ARC conversions.

You covered several topics in this tutorial, including converting a Cocos2D game from 1.x to 2.x. The Cocos2D site has a migration guide that discusses code migration from 1.x to 2.x in detail.

To learn more about ARC and various issues you might run into converting projects, check out Chapters 2 and 3 in iOS 5 by Tutorials, Beginning and Intermediate ARC.

I hope you enjoyed this tutorial, and have fun converting your own Cocos2D projects to run under Cocos2d 2.x and ARC! I’d love to hear about your own conversion experiences in the forums.


This is a post by Tutorial Team Member Tony Dahbura, an independent iOS developer with FullMoon Manor LLC. You can also find him on .

Tony Dahbura

Contributors

Tony Dahbura

Author

Over 300 content creators. Join our team.