50

I have an iOS app that I want to convert to using ARC. I would just use the migration tool, but it errors out for me consistently during the preview phase.

I know I can use the -fobjc-arc compiler directive on a file-by-file basis, but I want to enable ARC on the entire project, then turn off individual classes using -fno-objc-arc.

New projects in 4.2 can use ARC by default, so there must be a switch somewhere.

Can anyone help me to convert the project to ARC

tausun
  • 2,154
  • 2
  • 24
  • 36
Alpinista
  • 3,751
  • 5
  • 35
  • 46

3 Answers3

64

"ARC is available in Xcode 4.2, currently in beta, and only when compiling with Clang (a.k.a. "Apple LLVM compiler"). The setting is called, obviously enough, "Objective-C Automatic Reference Counting". Turn it on, and off you go.

If you're working on existing code, changing this setting will produce an enormous quantity of errors. ARC not only manages memory for you, but it forbids you from trying to do it yourself. It's illegal to manually send retain/release/autorelease when using ARC. Since normal non-ARC Cocoa code is littered with this stuff, you'll get a lot of errors.

Fortunately, Xcode offers a tool to convert existing code. Select Edit -> Refactor... -> Convert to Objective-C ARC... and Xcode will guide you through converting your code. Although there may be some situations where it needs help figuring out what to do, the process should be largely automatic."

I took that from this link, helped me a lot: http://www.mikeash.com/pyblog/friday-qa-2011-09-30-automatic-reference-counting.html

RyanG
  • 4,393
  • 2
  • 39
  • 64
  • Not "largely automatic" at all on my end, I just spent about an hour or more getting rid of retains and autoreleases, what is the point of this `Convert to Objective-C ARC`? What does it do? – PaulG Dec 02 '14 at 14:40
  • Are you correct, you still need to manually remove the retains and releases.. Im not sure if it was Apples goal to force you to see what you don't need anymore or if it was too error prone for Xcode to remove them all itself. – RyanG Dec 02 '14 at 14:58
47

Select your project or target and then go to Build Settings and under the section Apple LLVM compiler 3.0 - Language you will see the option Objective-C Automatic Reference Counting.

5

For towpse, in Xcode 4.6 you will need to look for "CLANG_ENABLE_OBJC_ARC" and then change it to "Yes" to enable.

Joel B
  • 736
  • 7
  • 11