How to automatically convert Manual Retain-Release code to ARC? - ios

I have thousands of line of code written for iOS 4. The codebase contains many calls to retain and release, which cause errors when the project is updated to iOS 5 and ARC.
Is there a way to automatically convert Manual Retain-Release (MRR) code to Automatic Reference Counting (ARC)?

From the Xcode 4.2 release notes:
To initiate the process, enable Continue building after errors in the
General Preferences pane, then choose Edit > Refactor > Convert to
Objective-C ARC. The targets that you convert are updated to build
using the Apple LLVM compiler. Xcode attempts to build your target and
to determine what changes must be made to use ARC. If it finds any
issues that prevent conversion, Xcode displays a dialog directing you
to review the errors in the Issue navigator. After you correct the
errors, choose the Convert to Objective-C Automatic Reference Counting
menu item again to restart the ARC-conversion workflow.

In Xcode 6+, the command is now:
Edit > Convert > To Objective-C ARC...

Related

How to fix: ARC forbids explicit message send of 'autorelease' compiler warnings?

After combining an Xcode project that was written in Swift with another Xcode project that was written in Objective C, the error is displayed in reference to a file that worked seamlessly in its native Objective C project. After adding a bridging header to the new project to allow interoperability between Swift and Objective C, the compile process has narrowed down to these 2 warning messages.
ARC forbids explicit message send of 'autorelease'
&
autorelease' is unavailable: not available in automatic reference counting mode
After some research, it seems that one of the solutions is to navigate to the projects
edit > convert > To Objective-C ARC...
After trying the above, it appears that the window for the arc options that pops up displays a virtually blank window.
Even after explicitly navigating to the target settings in Xcode, the pop-up window appeared blank as well.
The second option included turning off ARC for the project. After trying this, 3 new warnings occurred pertaining to lines of code from the native Objective C project that were not there in the native version.
How can this be fixed?

Migrating issue from non ARC to ARC with XCode 5.0.2

I heard about ARC and I also used it with XCode 4.2 but now a days I have requirement to convert my non-ARC code to ARC format. I have followed some steps to for converting my code to ARC format but somehow i did not get success.
I followed below steps to convert code to ARC:
1. “Preferences” -> “General” -> check “continue building after error”. This step is fundamental to avoid an huge waste of time repeating the next steps every time an error is encountered!
2. “Edit” -> “Refactor” -> “Convert to Objective-C ARC”
3. “Select targets to convert” (check them)
4. Click “precheck” – “Cannot Convert to Objective-C ARC” (Xcode found N issues that prevent conversion from proceeding. Fix all ARC readiness issues and try again.) You will see this message at least once, because your code contains calls that are forbidden by ARC.
5. Fix them using suggestions (click on errors to open the popup containing the tip). Then repeat from step 1.
6. “Convert to automatic reference counting” window will appear (once issues have been fixed)
7. Click next
8. Review automatic changes
I still get errors in all HEADER files of my project regarding #property and #synthesize. I have to manually do all the changes in all HEADER files rather than it can be achieved by automatically just following above steps.
Please help me and let me know if you have any proper solution for this one.
Thanks,
Nilesh M. Prajapati
XCode could not fix those errors automatically. So you just have to fix them manually before moving on.

IOS arc disabling for part of code [duplicate]

This question already has answers here:
How can I disable ARC for a single file in a project?
(18 answers)
Closed 9 years ago.
I'm a newbie to IOS technology. I have been developing an app, in which I have to add around 10,000 mkannotations to my map. So I prefer to use clustering algorithm (probably using k_means). However, I found this beautiful external library at (https://www.cocoacontrols.com/controls/adclustermapview)
When I complied the example framework of this project and ran it on simulator everything works fine, however, when I add the required files to my project as they described, it raises a lots of errors regarding
retain
release etc.
So I understand there is some issue with the arc compatibility in the project
Here are my questions
These retain, release methods being used in some project, does
that mean that they are developed with old versions of IOS?
If so, how do I resolve these error, other than manually removing them?
Is there a method in IOS, which allows me to compile a code
partially using one compiler and the remaining using other?
Select desired files at Target/Build Phases/Compile Sources in Xcode
PRESS ENTER
Type -fno-objc-arc
Press Enter or Done
In xcode Edit-Refactor-Convert to Objective-C ARC select your app then check button.Finally click save button.
These retain, release methods being used in some project, does that
mean that they are developed with old versions of IOS? NO
If so, how do I resolve these error, other than manually removing
them?
Is there a method in IOS, which allows me to compile a code
partially using one compiler and the remaining using other? You can disable ARC for some files.
This problem might be coming because you must be using ARC in your project while this framework is not. To remove ARC for particular classes go to Targets, then build phases tab, under the compile sources group add '-fno-objc-arc' as the compiler flag. This will make sure that ARC is skipped while compiling this class..

Transitioning one separate file to arc

I have a project that is already transitioned to ARC. Now I'm trying to include an existing file from the other project, which is not using ARC and I want it to be ARC-compliant too: release-retains, [super dealloc]s gone from this file, quick fixes, other stuff "Convert to Objective-C ARC..." does.
The problem is I can't use the Edit->Refactor->"Convert to Objective-C ARC..." tool for this. If I select only this file in "Select Targets to Convert" screen I'm getting "Cannot Convert to Objective-C ARC" message because of errors like: "#synthesize of 'weak' property is only allowed in ARC or GC mode". But they are already in ARC mode indeed! Also numerous warnings: "Method possibly missing a [super dealloc] call"
If I select all files except marked with -fno-objc-arc while converting, I get only errors about weak properties.
Of course I can build and delete the release-retains manually but why to walk if there is a bus (Conversion tool)... So can I auto-transition a separate file to ARC?
Update: I do not want ARC to be turned off for this file with -fno-objc-arc flag, I want ARC used in this file.
If you insist on making Xcode do the work, create a new Xcode project and deselect "Use ARC" when creating it. Add the files to convert, and convert the project to ARC. Take the modified files and import them into your other project.
It's probably simpler, however, to convert the file manually. This is not difficult, even for a large file or an entire project. Just lean on the compiler: build your app, walk through the errors and simply delete all retain/release calls and convert any NSAutoreleasePools to #autoreleasepool {}. You may also need to add __bridge casts if interacting with core foundation types.
It sounds like you'll have to convert this file to ARC manually, instead of relying on the automatic conversion tool. You'll need to go through the file and remove all of the release, retain etc. I've done this before, and while it takes a while, it's not too painful. Rely on the error messages from Xcode to guide you what needs to be fixed/removed in the code.
Here's a couple of links I found that may help. Also look at Apple's ARC docs and WWDC 2011 talks (referenced at the bottom of the second link).
Xcode ARC conversion tool issue
http://blog.mugunthkumar.com/articles/migrating-your-code-to-objective-c-arc/

Problems With an old Project on Xcode 4

I'm trying to compile an old project that I changed the UI a bit, but I've updated to Phonegap 1.0 and Xcode 4, so I'm getting this errors:
What should I do?
It looks like you now have ARC (Automatic Reference Counting) set on in the project. To use ARC your code will have to change, one of the changes is the way the autoreleasepool is created/drained.
Other things that are not available are retain. release and autorelease, the compiler generates all of these behind the scenes as necessary. This all works iff all of the Obj-C naming conventions have been followed.
Xcode has refactoring help to convert a project. However this many not be appropriate in your case, if that is true turn it off in the project/target build settings:
ARC can be disabled on a file-by-file basis:
There is no problem mixing ARC compatible files with non-ARC files. This can help bring legacy projects to ARC. ARC is the future.

Resources