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?
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
See this answer for more info:
Disable Automatic Reference Counting for Some Files
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
;)
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.
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
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!
Just use the -fno-objc-arc flag in Build Phases>Compile Sources
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
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 :
use -fno-objc-arc for each file in build phases
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
Please Just follow the screenshot and enter -fno-objc-arc .
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.
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
Add flag “-fno-objc-arc”.
Simple follow steps :
App>Targets>Build Phases>Compile Sources> add flag after class “-fno-objc-arc”
Just use the -fno-objc-arc flag in Build Phases>Compile Sources infront of files to whom you dont want ARC to be apply.
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.
select project -> targets -> build phases -> compiler sources
select file -> compiler flags
add -fno-objc-arc
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
Related
I am trying to use SDWebImage in an IOS project.
I am following these instructions from their github:
import the project as a static library.
Add the SDWebImage project to your project
Download and unzip the last version of the framework from the download page
Right-click on the project navigator and select "Add Files to "Your Project":
In the dialog, select SDWebImage.framework:
Check the "Copy items into destination group's folder (if needed)" checkbox
Add dependencies
In you application project app’s target settings, find the "Build Phases" section and open the "Link Binary With Libraries" block:
Click the "+" button again and select the "ImageIO.framework", this is needed by the progressive download feature:
Add Linker Flag
Open the "Build Settings" tab, in the "Linking" section, locate the "Other Linker Flags" setting and add the "-ObjC" flag:
Import headers in your source files
In the source files where you need to use the library, import the header file:
#import <SDWebImage/UIImageView+WebCache.h>
But I am getting this error:
SDWebImage is ARC only. Either turn on ARC for the project or use -fobjc-arc
I have tried to search for header search paths that some other answers say and I get these results:
Always search user paths -No
Header search paths
User header search paths
Must I set a value there? Or should I do anything else?
Another thing that i noticed in their demo is that: in their demo, in the frameworks folder they have imported the .xcodeproject of sdwebimageview and I have imported sdwebimageview.framework as the instructions say in the github.
Thanks!
Edit: my project is non-Arc.
I tried changing the other Linker flags from -ObjC to fobjc-arc but I still get the same error.
1)was that correct or should I change something else?
2) if I change my project from non-arc to arc what should I change? I only want to use that library so I prefer not change it.
In the .h file os SDWebImage it says:
#if !__has_feature(objc_arc)
#error SDWebImage is ARC only. Either turn on ARC for the project or use -fobjc-arc flag
#endif
This tutorial shows you how to set ARC or non-ARC per file in your project.
Unless you wanted to switch to ARC anyway, I suggest you leave your project as it is and just enable the -fobjc-arc option for the files in SDWebImage. Look at the tutorial above, the image in that shows you where to put the flags. (Their example uses -fno-objc-arc, which does the opposite, so just make sure you use -fobjc-arc. You type it in the same place.)
I want to check Internet connection in my app. So i use Apple's Reachability sample code.
But because of using ARC there are some errors. I correct some of them but other errors still in my code.
How should i solve this problems? Any ideas?
Thnx.
You can disable ARC on a per-file basis.
Select the project in the file list
Select the target
Click the Build Phases tab
Open the Compile Sources phase
Scroll down until you find your source file (or use the search field)
Double-click in the Compiler Flags column for that file
Enter -fno-objc-arc
Now ARC is disabled for that one file.
Try using this for your ARC version code -> Reachability (iOS) ARCified
I am working on Xcode 4.3.2 i wanted to rename my classes so i used refactor>rename, but after that i changed my mind and decided to go back to the older name. But when i do the same thing i've done before again, a warning dialog appears saying:
yourclass.h is not part of the build phase of any targets in this workspace and so can't be refactored. add the file to the build phase of a target in this workspace, or make a selection in another file.
what's the build phase? how can i solve this problem?
Thank you very much
It may be worthwhile to delete the projects derived data
Organizer --> Projects --> hit the delete derived data button for the project in question
Quit XCode
Open XCode
Build project. This will re-index your project and has been known to fix various environment issues such as
code sense
auto complete and
copmpiler targets
In Xcode, select your project at the top of the file tree on the left hand side.
From there, select your target, and click the "Build Phases" tab. Make sure the .m file of the class you're wanting to rename is listed under "Compile Sources"
You could check if you have the file here:
Targets => Select the target which is the select target your scheme is trying to run => Tab Build Phases => Compile Sources
Check if there is something wrong there (like file in red).
xCode usually takes snapshots when you rename the classes through the re-factor option .. you can go to File > restore SnapShote .. and choose the snapshot you want retrieve to and you will get back all the changed names.
In my case the .m file was under the compile sources. I did clear the project derived data and restarted xcode but to no avail.
I figured out though, that attempting to refactor the specific property in the .m file worked and renamed the property both in the header and the rest of the project references.
I have already created a project in XCode 4.2 with ARC checked. The problem is that I have to download and add the ASIHttpRequest files. This is giving a whole bunch of errors when I try to compile. Is there some way to fix this (other than to create a project again) ?
Click on the name of the project on the navigation view in the left side, go to Targets -> Build Phases and add -fno-objc-arc to the "compiler flags" for any relevant files.
You can turn off ARC per-file by passing the flag -fno-objc-arc to each as they are compiled. Add the flag to the files that need ARC disabled in the Compile Sources group under the target in the project navigator.
Another option would be to compile ASIHttpRequest as its own framework that doesn't have ARC enabled and just link against the framework. Speak of, isn't ASIHttpRequest already in a framework? (I've not looked at it in a while)
Keep using ARC, but disable it for the ASIHttpRequest files. This answer should point you in the right direction:
How can I disable ARC for a single file in a project?
how can i remove the source code from my static library with XCode 4?
When i have Xcode 3 in target there is the list of source and i can delete them, but with XCode 4 the target is not expandable...
Can i do olso with some libtool?
Thansk!
Not sure to understand what you mean, but if you want to remove symbols from a binary/library, you should use the strip command.
A static library is not source code, it's machine code.
You can't have access to the source files, if you only have a static library.
Otherwise, if you meant "target" instead of library, meaning you are actually building a library with Xcode, and you want that specific source files don't compile into the library, use the "Target membership" section on the Xcode right panel (utilities).
When you select the file, you can set the target for the source file:
I'm assuming that you have the library sources added to the project and you want to remove them from a specific target. Select the project (the top level item on the left side), select the target under "Targets", click "Build Phases" and remove the respective items from "Compile Sources".
Under build settings for the static library target, select NO for 'deployment postprocessing' and 'strip debug symbols during copy'. It is compiled code so it doesn't need symbols stripped.