Convert App to ARC while ignoring files? - iphone

I want to convert my app to ARC but I have some external librarys and frameworks that are not complaint with ARC. I know when yous start a new project using ARC, you can later specify which files to ignore. But can you do this when upgrading an existing app to ARC?

in XCode, go to your active target and select the Build Phases tab. In the Compiler Flags column, set -fno-objc-arc for each of the files you don't want ARC
EDIT:
Also, to have the opposite behavior, I mean to use a file that was written using ARC inside a non ARC project, you can set the file flag to -fobjc-arc instead
There is plenty of more info regarding ARC here: http://clang.llvm.org/docs/AutomaticReferenceCounting.html#general

ARC may be explicitly enabled with the compiler flag -fobjc-arc. It may also be explicitly disabled with the compiler flag -fno-objc-arc. The last of these two flags appearing on the compile line "wins".
Source: http://clang.llvm.org/docs/AutomaticReferenceCounting.html

To add to Felipe's answer, if you want to add this to multiple files at once:
Select all the files you want to exclude form ARC
Press 'Enter'
The pop-up box will appear at the last file in the list
Type -fno-objc-arc

Related

Use Automatic Referencing Count in specific classes in ios

Actually i came to this question when i was trying to add some classes that have been made upon ios prior to IOS 5 and these classes doesn't having ARC and the project i am trying to add is made upon the IOS 5 and it give me the compile time error related to ARC the classes having suck kind of information that if i try to remove the release/retain then it start behaving irregular.That is my problem, Now come to question i want to know that is there any way so that i can mark those classes not to use ARC so that the newly created classes that having base SDK ios5 compile with ARC and i mention not to use ARC simply compiled with their retain/release values.That is the only way i have left i think for making properly this app.
Any idea how i can use those classes that is having base sdk prior to ios5.
Thanks,
the image below will show you how to do it.
-fno-objc-arc flag to disbale arc
-fobjc-arc flag to enable arc
Go to your project settings, under Build Phases > Compile Sources
Select the files you want ARC disabled and add -fno-objc-arc compiler flags. You can set flags for multiple files in one shot by selecting the files then hit Enter key.
Go to Issue Navigator -> Validate project settings -> Perform changes. In this way Xcode automatically remove release and retain keywords from whole project and convert into ARC compatible.

Adding the tag to a ARC enabled project

In a ARC enabled project, is there a way to add the -fno-objc-arc tag programatically in the code it self.
So i could give my source files to someone else, and that person need not add the -fno-objc-arc manually.
I assume this is because you want to distribute your project as a re-usable library that can be used in other projects regardless of whether they use ARC?
I don't think you can add the flag to the source to tell ARC to ignore the file (at least I've not found a way yet), but you can detect if ARC is enabled within the file using
#if __has_feature(objc_arc)
...
#endif
You could use this to raise a warning, by saying
#warning This file is not ARC compatible, add the -fno-objc-arc tag
But an even more elegant solution is to use this feature to automatically branch your code at compile time so that it works on both ARC and non-ARC builds.
I wrote a simple re-usable header that can be pasted into the top of your source file or included in the project as a standalone header file:
https://gist.github.com/1563325
This provides a bunch of macros to use instead of retain, release and autorelease methods so that they can be automatically stripped for ARC builds. For an example of how this is used, check out iRate:
https://github.com/nicklockwood/iRate/tree/master/iRate
It only takes a few minutes to convert a class file this way.
No - not in the code itself. This is stored in your project file, so if you send the entire project to someone else it should work. However, you can't just send them a few classes and have that work (it needs to be the entire project).

How can I disable ARC for an entire group/folder of files?

I'm aware that you can set the compiler flag -fno-objc-arc to disable Automatic Reference Counting (ARC) for each file in Compile Sources in XCODE but how can I do this for an entire group of files without having to do each file at a time?
The reason I ask is that I've added a large library to my application and I need to set the -fno-objc-arc compiler flag for over 100 files.
Goto Build Phases -> Compile sources select multiple files holding command ⌘ and press enter then add the value -fno-objc-arc It will reflect for all the selected files.
I do not think that XCode lets you set this flag for a folder or a group, but you have two reasonable alternatives to setting the flag on individual files:
If your library is self-contained, you can create a separate project for it in the same workspace as your application, and disable ARC for that project.
If you are OK modifying the source of that library, you can attempt an automated ARC conversion.

How to add ARC for specific file?

I created a project without ARC. I want to use a third party SDK in my project. That SDK comes with ARC Support. So I want to add ARC for that third party files. I can disable ARC for all "MYProject" files by adding this flag into build phases like -fno-objc-arc.
But I may use large number of files. So it's better to add ARC for specific SDK only. So how to add ARC support for single or specific SDK files in XCode Project?
For Instance: I want to use Grid View. I'm adding GMGridView in my project and it comes with ARC support.
Add the flag -fobjc-arc to the files that you'd like to use ARC. It's just the opposite of the other.
You add compiler flags in Targets -> Build Phases -> Compile Sources
Compile source show all file that project contain.so please add -fobjc-arc to compiler flag by double clicking on it as show in below image.

Files doesn't support the ARC feature, how to deal with

My project uses the ARC(Automatic Reference Counter), so when i try to import the ASIHTTPRequest library which has 4 classes that used in their implementation the autorelease statement, here is an example:
[self setAccessLock:[[[NSRecursiveLock alloc] init] autorelease]];
The error that i got:
ARC forbids explicit message send of autorelease
What should i do to solve this problem.
I have got the solution, i needed to add the right flags to the compiler flags for each file which doesn't support the ARC. For me, i use Xcode 4.2, so under target -> Build Phases -> Compile Sources, double click on the file you want to disable the ARC feature and put -fno-objc-arc the click on Done. And it's Done ;)
Please note, that if the file doesn't exist on the list, you can just add it by clicking the + button and then perform the same work above.
Disable ARC for that one class
When you migrate a project to use ARC, the -fobjc-arc compiler flag is set as the default for all Objective-C source files. You can disable ARC for a specific class using the -fno-objc-arc compiler flag for that class.
In Xcode, in the target Build Phases tab, open the Compile Sources group to reveal the source file list. Double-click the file for which you want to set the flag, enter -fno-objc-arc in the pop-up panel, then click Done.