ChatGPT解决这个技术问题 Extra ChatGPT

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

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?


C
Community

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

https://i.imgur.com/BgteG.png

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


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.
Can you specify for a directory, i.e., for all files in a directory? What about for a Group?
No directories. Only one single file at a time in the build settings :(
@casademora you cannot get enough upvotes for this piece of information. Thank you.
@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 :(
T
Tibidabo

Disable ARC on MULTIPLE files:

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

;)

https://i.stack.imgur.com/JVrWg.png


Thx! Been looking for this feature a long time. Double-clicking doesnt work when selected multiple rows, but pressing enter does. So weird.
Oh wow. I'd spend all my karma to upvote this again. Thanks for sharing!
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?
I had 4.3.3 and now 4.4 DP7 and it still works just fine. Try restarting Xcode, restarting machine and repair permissions.
J
Jingjie Zhan

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.


s
swiftBoy

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

Follow below steps :

Disable ARC on individual file:

Select desired files at Target/Build Phases/Compile Sources in Xcode 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


d
digipeople

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

open "Build phases" -> "Compile sources" select files with "left_mouse" + "cmd" (for separated files) or + "shift" (for grouped files - select first and last) press "enter" paste -fno-objc-arc press "enter" again profit!


S
Serghei Pogor

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


u
user2538944

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

https://i.stack.imgur.com/HCp0N.png


J
Jayprakash Dubey

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 :

https://i.stack.imgur.com/bGKPq.png


M
Manish Jain

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


u
user2554822

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

T
Tunvir Rahman Tusher

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

https://i.stack.imgur.com/oG8w9.png


g
greenland

https://i.stack.imgur.com/0KAd4.png


V
Vin

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:

Select desired files at Target/Build Phases/Compile Sources in Xcode PRESS ENTER Type -fobjc-arc Press Enter or Done


M
Manoj Chandel

Add flag “-fno-objc-arc”.

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


C
C_compnay

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


a
adijazz91

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.


your answer does not provide any addition to this old question.
M
Mnsd

select project -> targets -> build phases -> compiler sources select file -> compiler flags add -fno-objc-arc


a
annu

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


your answer does not provide any addition to this old question.
What addition need. if you know better than provide us.
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...
can you give me some brief on it.