1357

I am using ARC successfully in my project. However, I have encountered a few files (e.g., in unit tests and mock objects) where the rules of ARC are a little more fragile right now. I recall hearing that there was a way to disable ARC on a per-file basis, though I have been unable to find this option.

Is this possible? How do I disable ARC on a per-file basis?

Nikolai Ruhe
  • 79,600
  • 16
  • 173
  • 195
casademora
  • 60,192
  • 15
  • 66
  • 77
  • http://stackoverflow.com/questions/30274209/why-maintaining-retain-count-is-not-required-in-arc-enabled-project?__=2075832026 – Vizllx Feb 22 '16 at 10:02

18 Answers18

2176

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

You add compiler flags in Targets -> Build Phases -> Compile Sources. You have to double click on the right column of the row under Compiler Flags. You can also add it to multiple files by holding the cmd button to select the files and then pressing enter to bring up the flag edit box. (Note that editing multiple files will overwrite any flags that it may already have.)

I created a sample project that has an example: https://github.com/jaminguy/NoArc

xcode

See this answer for more info: Disable Automatic Reference Counting for Some Files

Community
  • 1
  • 1
jaminguy
  • 25,490
  • 2
  • 20
  • 21
  • 58
    So, the place to add this flag isn't too intuitive. But, in Xcode 4, in build phases, and compile sources, there are actually two columns. One for the file name, and another for the compile flags for that one file. This is where you enter this specific compiler flag. – casademora Jul 26 '11 at 17:58
  • 3
    Can you specify for a directory, i.e., for all files in a directory? What about for a Group? – ma11hew28 Aug 09 '11 at 13:45
  • 4
    No directories. Only one single file at a time in the build settings :( – casademora Aug 09 '11 at 15:15
  • 13
    @casademora you cannot get enough upvotes for this piece of information. Thank you. – theTRON Oct 28 '11 at 00:06
  • 2
    Yes, this works but Xcode 4.2.1 still generates unsightly ARC errors on the editor window, although it eventually compiles just fine. You might want to put all your non-ARC files into a static library and link that to your main target. – adib Dec 04 '11 at 14:05
  • I did this but Xcode refactor cleans my flag and throws errors in the files. I run the refactor convert to ARC and the flag is not there anymore. Any help – Tony Jan 13 '12 at 15:07
  • 1
    @adib - restarting XCode after adding the compiler flag got rid of the ARC errors for me. – fishinear Mar 07 '12 at 18:14
  • 26
    @casademora you can select multiple files at once - but then don't doubleclick but hit RETURN. then just add the `-fno-objc-arc` and it will be added to all selected files. **edit** just saw the answer below this one :( – Philipp Kyeck Apr 07 '12 at 13:08
  • After applying this compiler flag I was still getting errors. I had to click Product -> Clean and then I was able to rebuild my project successfully. – Mike May 14 '12 at 02:39
  • thanks a lot. just for people who didn't know, if you can't find the file you want in the compile source, press the "+" button to add files – SamChen Jun 06 '12 at 22:16
  • Note on **multiple flags**: use **spaces** to separate them. – eduludi Oct 27 '15 at 11:33
  • 1
    Note that editing multiple files will overwrite any flags that it may already have. – Roberto Nov 11 '15 at 03:46
  • This works for me after adding complier flag I restart Xcode.and got rid of ARC. – Neha Ghetia Jan 30 '17 at 11:11
461

Disable ARC on MULTIPLE files:

  1. Select desired files at Target/Build Phases/Compile Sources in Xcode
  2. PRESS ENTER
  3. Type -fno-objc-arc
  4. Press Enter or Done

;)

enter image description here

Tibidabo
  • 20,865
  • 4
  • 85
  • 85
  • 2
    Thx! Been looking for this feature a long time. Double-clicking doesnt work when selected multiple rows, but pressing enter does. So weird. – bobmoff Apr 12 '12 at 13:31
  • 4
    Oh wow. I'd spend all my karma to upvote this again. Thanks for sharing! – jemmons May 04 '12 at 00:59
  • this seems not work on Xcode 4.3.3 anymore. double click or press enter on the selected files doesn't bring up the dialog for putting in the -fno-ojbc-arc flag, anyone has a workaround for this? – tom Jul 25 '12 at 17:43
  • I had 4.3.3 and now 4.4 DP7 and it still works just fine. Try restarting Xcode, restarting machine and repair permissions. – Tibidabo Jul 29 '12 at 07:28
44

For Xcode 4.3 the easier way might be: Edit/Refactor/Convert to objective-C ARC, then check off the files you don't want to be converted. I find this way the same as using the compiler flag above.

Jingjie Zhan
  • 965
  • 8
  • 12
37

It is very simple way to make individual file non-arc.

Follow below steps :

Disable ARC on individual file:

  1. Select desired files at Target/Build Phases/Compile Sources in Xcode

  2. Select .m file which you want make it NON-ARC PRESS ENTER Type -fno-objc-arc

Non ARC file to ARC project flag : -fno-objc-arc

ARC file to non ARC project flag : -fobjc-arc

swiftBoy
  • 33,793
  • 26
  • 129
  • 124
Dipak Narigara
  • 1,738
  • 16
  • 18
35

Note: if you want to disable ARC for many files, you have to:

  1. open "Build phases" -> "Compile sources"
  2. select files with "left_mouse" + "cmd" (for separated files) or + "shift" (for grouped files - select first and last)
  3. press "enter"
  4. paste -fno-objc-arc
  5. press "enter" again
  6. profit!
digipeople
  • 732
  • 6
  • 9
22

Just use the -fno-objc-arc flag in Build Phases>Compile Sources

Serghei Pogor
  • 108
  • 1
  • 10
Rahul Gupta
  • 973
  • 11
  • 15
13
  1. Select Xcode project
  2. Go to targets
  3. Select the Build phases section
  4. Inside the build phases section select the compile sources.
  5. Select the file which you do not want to disable ARC and add -fno-objc-arc

Disable ARC for a particular file

user2538944
  • 285
  • 4
  • 12
13

It is possible to disable ARC (Automatic Reference Counting) for particular file in Xcode.

Select Target -> Build Phases -> Compile Sources -> Select File (double click) -> Add "-fno-objc-arc" to pop-up window.

I had encountered this situation in using "Reachibility" files.

This is shown in below figure :

enter image description here

Jayprakash Dubey
  • 32,447
  • 16
  • 161
  • 169
11

use -fno-objc-arc for each file in build phases

Amit Agrawal
  • 849
  • 3
  • 13
  • 27
11

The four Mandatory Step as explained in this video

    //1. Select desired files
    //2. Target/Build Phases/Compile Sources
    //3. Type -fno-objc-arc
    //4. Done
user2554822
  • 181
  • 2
  • 7
8

Please Just follow the screenshot and enter -fno-objc-arc .

enter image description here

Tunvir Rahman Tusher
  • 5,542
  • 2
  • 33
  • 29
7

If you're using Unity, you don't need to change this in Xcode, you can apply a compile flag in the metadata for the specific file(s), right inside Unity. Just select them in the Project panel, and apply from the Inspector panel. This is essential if you plan on using Cloud Build.enter image description here

greenland
  • 279
  • 3
  • 6
5

Add flag “-fno-objc-arc”.

Simple follow steps : App>Targets>Build Phases>Compile Sources> add flag after class “-fno-objc-arc”

Manoj Chandel
  • 498
  • 5
  • 12
5

I think all the other answers are explaining how to disable MRC(Manual Reference Count) and enabling ARC(Automatic Reference Count). To Use MRC(Manual Reference Count) i.e. Disabling ARC(Automatic Reference Count) on MULTIPLE files:

  1. Select desired files at Target/Build Phases/Compile Sources in Xcode
  2. PRESS ENTER
  3. Type -fobjc-arc
  4. Press Enter or Done
Vin
  • 440
  • 7
  • 18
2

Just use the -fno-objc-arc flag in Build Phases>Compile Sources infront of files to whom you dont want ARC to be apply.

C_compnay
  • 123
  • 1
  • 4
-1
  1. select project -> targets -> build phases -> compiler sources
  2. select file -> compiler flags
  3. add -fno-objc-arc
Mnsd
  • 37
  • 5
-2

GO to App -> then Targets -> Build Phases -> Compile Source

Now, Select the file in which you want to disable ARC

paste this snippet "-fno-objc-arc" After pasting press ENTER

in each file where you want to disable ARC.

adijazz91
  • 302
  • 2
  • 12
-2

Following Step to to enable disable ARC

Select Xcode project Go to targets Select the Build phases section Inside the build phases section select the compile sources. Select the file which you do not want to disable ARC and add -fno-objc-arc

annu
  • 139
  • 4
  • 12
  • 1
    your answer does not provide any addition to this old question. – jomafer May 14 '15 at 08:54
  • What addition need. if you know better than provide us. – annu May 14 '15 at 09:46
  • I didn't downvote you. It is just this question was asked in Jul 11 '11 at 5:21. There are too many answers with the same answer that don't add any useful information...I suspect that someone downvote you for that reason... – jomafer May 14 '15 at 09:56
  • can you give me some brief on it. – annu May 14 '15 at 12:48