183

I have downloaded the iOS 5 SDK and found that ARC is a great feature of the new Apple compiler. For the time being, many third party frameworks don't support ARC. Could I use ARC for my new code and keep the current retain/release code unchanged? The ARC converter doesn't work here, because some frameworks, such as JSONKit, cannot be converted to ARC by using the converter.

Edit:

The answer is to add -fno-objc-arc to the compiler flags for the files you don't want ARC. In Xcode 4, you can do this under your target -> Build Phases -> Compile Sources.

5
  • 10
  • 2
    To clarify, you just add it to the .m file. Not the .h file.
    – ma11hew28
    Aug 18, 2011 at 18:15
  • I found this conversation: github.com/gowalla/AFNetworking/issues/36 Maybe it help someone how to integrated JSONKit in a iOS5 ARC Supported Project ;-) But i have not try it at the moment...
    – user998652
    Oct 17, 2011 at 6:48
  • This method worked for me. I was adding the AsyncUdpSocket for UDP Multicast to my project and those files [at least the current version] are NOT supporting ARC. So I used the method above to add the compiler flag to the AsyncUdpSocket.m file and I can get the code to work fine with my application.
    – Ashu Joshi
    Mar 1, 2012 at 23:21
  • 2
    This works perfectly! Specifics: Go to your Target and choose Build Phases, then Compile Sources. If you double-click on the .m file in question, it'll pop up a box where you can type/paste: "-fno-objc-arc"
    – mpemburn
    Jul 11, 2012 at 13:50

4 Answers 4

78

It is not very intuitive how to disable ARC on MULTIPLE files, for a while I was do it one by one until a figured out how to do that.

  1. Select desired files at Target/Build Phases/Compile Sources in Xcode (CMD+click or Shift+click)
  2. PRESS ENTER (double click will reset the selection, so it does't work)
  3. Type -fno-objc-arc
  4. Press Enter or Done
2
  • 1
    It is not really an answer to the question asked here, but a bloody good tip anyway! Voted +1 Apr 25, 2012 at 21:44
  • You are right, it's not, but I always just googled for the switch and this answer one of the top hit. I thought these days most people already know the answer but not this "tip"
    – Tibidabo
    Apr 26, 2012 at 2:26
73

The public ARC docs, while not directly clear on this point, seem to suggest that as long as each class is either all ARC or all manually-managed, the classes can be integrated into a single program.

You only can't mix ARC and non-ARC in a single class; the document says that sending retain, release, autorelease, or retainCount messages by any means (including timers and delayed performs, which use @selector) is banned in ARC code. So you can't do non-ARC in an ARC class (because the necessary messages are banned) and you can't do ARC in a non-ARC class (because ARC adds syntax elements that are invalid without ARC).

The same document is a bit clearer on whether you can integrate non-ARC libraries/frameworks into an ARC program: Yes. It doesn't mention whether you can use ARC libraries/frameworks in a program where all your code is non-ARC, but given all of the above, the implication seems to be yes.

5
  • Thank you! I have figured out how to disable ARC for existing non-ARC classes.
    – nonamelive
    Jun 23, 2011 at 6:05
  • Could you share with us what you are doing?
    – David H
    Jul 29, 2011 at 12:12
  • 1
    @David H: If you meant to address that to the questioner, you should note that the questioner edited their solution into the question. Jul 29, 2011 at 12:59
  • ARC compilation cannot be disabled in HEADER files. Clang will NOT compile the interface/header based upon the compilation of it's implementation/method file. Therefore, Header files must be compatible with both ARC and non-ARC in a mixed compilation. I view this as a bug in the compiler.
    – carmin
    Jan 24, 2013 at 22:33
  • 1
    @carmin: It isn't. The compiler does not look at header files except when you tell it to, which you normally only do by #importing them from module files. The compiler flags setting is for each module file (more precisely, each file in the “Compile Sources” build phase). The compiler has no way to know whether it should use ARC or not for any other file, except by inheriting the choice it was given for the module file it started with. Jan 25, 2013 at 1:46
35

If you want to disable Automatic Reference Counting for some Files then its really simple to do just follow the steps.You add compiler flags in Targets -> Build Phases -> Compile Sources.

xcode

The flag used is -fno-objc-arc press enter after writing it.! You have to double click on the right column of the row under Compiler Flags. Hope it helps Thanks :)

2
  • 14
    You can use the same method to enable ARC on a specific file in a non-ARC project by using the flag -fobjc-arc.
    – lnafziger
    Apr 21, 2012 at 4:12
  • ARC compilation cannot be disabled in HEADER files. Clang will NOT compile the interface/header based upon the compilation of it's implementation/method file. Therefore, Header files must be compatible with both ARC and non-ARC in a mixed compilation. I view this as a bug in the compiler.
    – carmin
    Jan 24, 2013 at 22:34
2

It is possible to disable ARC for individual files by adding the -fno-objc-arc compiler flag for those files.

How can I disable ARC for a single file in a project?

Not the answer you're looking for? Browse other questions tagged or ask your own question.