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

Objective CXcodeAutomatic Ref-CountingManual Retain-Release

Objective C Problem Overview


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?

Objective C Solutions


Solution 1 - Objective C

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: https://stackoverflow.com/questions/6448874/automatic-reference-counting-for-some-files

Solution 2 - Objective C

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

Solution 3 - Objective C

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.

Solution 4 - Objective C

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

Solution 5 - Objective C

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!

Solution 6 - Objective C

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

Solution 7 - Objective C

  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

Solution 8 - Objective C

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

Solution 9 - Objective C

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

Solution 10 - Objective C

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

Solution 11 - Objective C

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

enter image description here

Solution 12 - Objective C

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

Solution 13 - Objective C

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

Solution 14 - Objective C

Add flag “-fno-objc-arc”.

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

Solution 15 - Objective C

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

Solution 16 - Objective C

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.

Solution 17 - Objective C

  1. select project -> targets -> build phases -> compiler sources
  2. select file -> compiler flags
  3. add -fno-objc-arc

Solution 18 - Objective C

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestioncasademoraView Question on Stackoverflow
Solution 1 - Objective CjaminguyView Answer on Stackoverflow
Solution 2 - Objective CTibidaboView Answer on Stackoverflow
Solution 3 - Objective CJingjie ZhanView Answer on Stackoverflow
Solution 4 - Objective CDipak NarigaraView Answer on Stackoverflow
Solution 5 - Objective CdigipeopleView Answer on Stackoverflow
Solution 6 - Objective CRahul GuptaView Answer on Stackoverflow
Solution 7 - Objective Cuser2538944View Answer on Stackoverflow
Solution 8 - Objective CJayprakash DubeyView Answer on Stackoverflow
Solution 9 - Objective CManish JainView Answer on Stackoverflow
Solution 10 - Objective Cuser2554822View Answer on Stackoverflow
Solution 11 - Objective CTunvir Rahman TusherView Answer on Stackoverflow
Solution 12 - Objective CgreenlandView Answer on Stackoverflow
Solution 13 - Objective CVinView Answer on Stackoverflow
Solution 14 - Objective CManoj ChandelView Answer on Stackoverflow
Solution 15 - Objective CC_compnayView Answer on Stackoverflow
Solution 16 - Objective Cadijazz91View Answer on Stackoverflow
Solution 17 - Objective CMnsdView Answer on Stackoverflow
Solution 18 - Objective CannuView Answer on Stackoverflow