Is it too early to use ARC for new projects? - objective-c

I'm about to start a new project and I'm wondering whether I should use ARC or not.
I don't know if it's recommended to use ARC when I know that the new project will have quite a lot of dependencies on older code and external libraries which have not been converted to ARC yet (three20, shareKit, ASIHTTPRequest,..) ?
Some of the libs are quite big so I think it'll be tedious to add the -fno-objc-arc flag for each separate file.
It seems that Apple has went the ARC way definitely for IOS and every IOS developer will have to convert sooner or later.
But is it maybe just too early to jump on the ARC bandwagon yet ?

Compiled libraries will work with ARC out of the box. All ARC does is add the memory management automatically. It'll add the retains, releases etc. If the code is already compiled, it'll contain the necessary memory management, regardless of whether it was compiled with ARC or not.
If you're adding 3rd party code directly to your application, then you'll need to make sure that code works with ARC, but you don't need to change anything for compiled libraries and frameworks.
Apple are pushing ARC as the way forward, so for a new project, I don't think there should be any problems with using it.

Related

How do you get the 'use automatic reference counting' checkbox to show when starting a new mac application in xcode?

I just started learning about mac application development by doing this tutorial https://developer.apple.com/librarY/mac/referencelibrary/GettingStarted/RoadMapOSX/books/RM_YourFirstApp_Mac/Articles/GettingStarted.html
I feel dumb because I am stuck on the very first step. It says to make sure that the "use automatic reference counting" checkbox is checked, but I don't even have that checkbox.
This is a screenshot from the tutorial:
and this is a screenshot from my laptop while following along with the tutorial:
Since I am not even yet sure what automatic reference counting is I feel stuck because I don't know if it is important or not. Does anyone know why this option is not shown in my xcode?
The quick version: automatic reference counting is now the default, so just go to the next step.
The longer version: That tutorial appears to be written using Xcode 4.4 in the examples. The current version of Xcode is 5.1, and Apple has made a bunch of changes between then and now, some cosmetic and some more substantial.
One of those changes is that automatic reference counting (ARC) is now the default choice for new projects. ARC is a method of memory management where the compiler and runtime work together to keep track of which objects are still in use, and release the ones which aren't. When this technology was first introduced, it had some rough spots, and so it made sense to make it optional.
Apple is really pushing everyone to use ARC for all projects, so they removed the checkbox in the create project dialog. I'm not sure when this happened; may have been Xcode 5.0 or 5.1.
All that said: the tutorial you're using was written for an earlier version of Xcode than you're using. The big concepts are all going to still apply, but you'll run into issues from time to time where the screenshots don't look the same or the options are slightly different. So take a deep breath and just go for it. :)

upgrading to Automatic Reference Counting and cleaning up my code

I have a project from 2 years ago that I'd like to bring back into action in xcode.
Is there a simple way to do away with the old style memory management I have in there with retain/releases and use the new .. ARM?
And this is a 32bit app using the QTKit and QuickTime frameworks - can this become a 64bit app that I can put on the App Store one day if it works well?
Also I realise that my app is one enormous .h and .m file - i'd like to break it into smaller files to make it easier to read and find what i am doing - any tips on how to do that?
Thanks guys!
Adam
Use the Xcode menu item Edit->Refactor->Convert To Objective-C ARC which can automatically convert everything to ARC.
You can also use Edit->Refactor->Convert to Modern Objective-C syntax to update the code to use the new array/dictionary/number literal syntax.
For breaking out one file into many, Xcode's built-in refacting support is pretty terrible. You can try out Jetbrains Appcode which has much better support for this kind of thing.

ARC and ABAddressBook API for iOS

I have an app that uses the ABAddressBook API and I am trying to convert my app to use ARC.
I know the AddressBook API is written in native C and its giving me a lot of errors when ARC is enabled.
Anybody knows how to do it or is it even worth to do it?
Thanks in Advance.
One possible solution is to set compiler flags for the code that is giving you trouble to convert to. Go to Build Settings for the particular target you are using and add the flag "-fno-objc-arc" to the files that are problematic. Also, any more info on the actual problems arising would be helpful.
bartender,
First, working code is working code. You can easily intermix both kinds of memory management mechanisms in the same app. I only convert classes to ARC as I open the code up for other purposes. This allows me to slowly move my app to ARC. Why do you want to change this code to use ARC?
Second, the interface between core foundation items and ARC can appear to be complex ... but it isn't. Most issues are handled by using the __bridge type qualifier. Typically, the compiler/fix-it tells you exactly what you need to do. (Working, non-leaking CF code doesn't need to change. It just needs to tell ARC to leave it alone.)
Andrew

Do i need to release/autorelease my stuff for iphone

Hear a rumor that the new version of xcode is going to add the release statements for you.
So if I init something do I actually have to release it?
What you're referring to is called Automatic Reference Counting. ARC is a feature in recent versions of the LLVM compiler that will be included in a future version of Xcode. You have to specifically enable ARC to get the benefits from it, and it includes its own set of rules that you need to follow. Right now, unless you're compiling with a prerelease toolset, you need to do your own retains and releases. And even when the feature is released, you will need to specifically code for it — Xcode won't just helpfully correct your mistakes.

xcode 4 garbage collection removed?

I am learning Xcode and objective c, but the book I am reading is using the older version (3.2) of Xcode. One chapter, which is completely dedicated to memory and leaks, says I need to turn on garbage collection through Project Settings > Build. However, I can't find the garbage collection setting. Has it been removed or am I just missing it?
Xcode 4 still has Garbage Collection, all you do is go into your project (the top option in the navigation bar with the big xcode project icon). Select your target which should just be the name of your app with the application icon next to it. When you select that, along the top there is a bar with different options, such as summary, info, build settings etc. Hit "Build Settings" and type "garbage" in the search field. Then set garbage collection to required.
One very important note about GC is that it is officially deprecated as of Mac OS 10.8, which means you should be transitioning your code to ARC if possible because GC might not be around for much longer.
Update: See other answers for enabling GC in current versions of Xcode. As I stated above, you should be transitioning to ARC if you haven't already.
As of XCode 4.4 the garbage collection build flag is user defined (see the very end of the build settings list).
GCC_ENABLE_OBJC_GC supported
Valid build settings are:
unsupported: The application cannot load code that requires garbage collection. The loadable bundle cannot be loaded by an application that requires garbage collection.
supported: The application can load code that supports or requires garbage collection. The loadable bundle can be loaded by an application with any level of garbage-collection support.
required: The application can load only code that supports garbage collection. The loadable bundle can be loaded only by an application that supports garbage collection.
Garbage collection is very much fully supported in Xcode 4. Xcode 4, itself, is a GC'd application.
GC is also very much not available in iOS. If you are writing an iPhone/iPad app, you can't use GC.
That's a challenge but you might be able to get it done - here's a starting point to think about in relation to PDF generation:
iOS SDK - Programmatically generate a PDF file
If you can generally follow what is going on there, you might be OK.
Xcode 3.2 is still available for download - in fact, 3.2.6 was released within the past week or two, so it's not exactly "old and busted." :-) Xcode 4's interface is very different, so given your tight schedule and your need to start from square one, you might be better off using 3.2 for now. That will certainly make it far easier to use the book you have.
As of Xcode 4.3.3 the only setting available for parameter "Objective-C Garbage Collection" under section "Apple LLVM compiler 3.1 - language", Build Settings for your target app = "Unsupported"