109

As a beginning iPhone programmer, what is the best practice for writing apps to be used either with iOS 5 or older versions? Specifically, should I continue using the release/retain of data, or should I ignore that? Does it matter?

Roberto Canogar
  • 1,598
  • 11
  • 10
Geekgirl
  • 1,322
  • 3
  • 11
  • 17
  • 1
    Use ARC, and follow the best practices as outlined here: http://amattn.com/2011/12/07/arc_best_practices.html If you do that, you will find that ARC "just works". If you don't follow those practices, you'll end up with leaks and spend lots of time tracking them down.... – n13 May 04 '13 at 08:21

7 Answers7

170

For anyone still curious about how to turn off ARC on individual files, here's what I did:

  1. Go to your project settings, under Build Phases > Compile Sources
  2. 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 hitting "Enter" key.

I don't know if this is the recommended way, but it works for me.

PS: I gathered this information from clang.llvm.org here which is publicly accessible, thus not under NDA.

Balaji Kandasamy
  • 4,446
  • 10
  • 40
  • 58
pixelfreak
  • 17,714
  • 12
  • 90
  • 109
  • 1
    When I use this flag with a library it works, but as soon as I include the lib .h file into an ARC class Xcode complains as if I didn't have the flag there. Were you able to get older libraries to work with this flag? – casey Jun 24 '11 at 04:31
  • I am able to get ASIHttpRequest and SBJson working (I get 1 warning on struct usage in Reachability.h). I put the flag in all their implementation files. – pixelfreak Jun 24 '11 at 22:37
  • All the implementation files or just the .h's? In my Compile Sources section of the project it only has the header files, no implementation files. I can add them, but it doesn't *seem* to have a different effect. For your reference, I am trying to get a REST parser to work. (https://github.com/mirek/NSMutableDictionary-REST.framework) – casey Jun 25 '11 at 00:04
  • 1
    Aha! I did not look closely enough at the errors. They are different errors than without the flag. I just had to remove the auto release pool from the code and wha-la! – casey Jun 25 '11 at 00:09
  • When I select multiple files and hit enter, as it was suggested here, they were all removed from the 'Compile sources' in Build phases. I had to select them individually. Not sure if I'm doing something wrong. – Gopalakrishnan Subramanian Apr 06 '12 at 18:46
  • I love how intuitive Xcode 4.x is. Just press enter to type the flags. Amazing. Thanks Apple. – Duck Jul 28 '12 at 00:40
98

It's up to you. You can write apps using ARC (Automatic Reference Counting), and Xcode will write "glue code" to allow your ARC enabled apps to run on iOS 4, no modifications required. However, certain things wont work, and most noticeably many libraries you might wish to use will (sometimes) throw up innumerable errors and you will be unable to use them until the developers release an update which is compatible with ARC.


Edit: I recently discovered that you can turn off ARC on a per-file basis. See pixelfreak's answer. So, my advice still stands, but now the 3rd-party libraries shouldn't need to be updated to work with ARC.

Here's what Apple says about opting out of ARC for specific files:

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.

enter image description here

See the full transition guide here.

Community
  • 1
  • 1
sudo rm -rf
  • 29,408
  • 19
  • 102
  • 161
  • 15
    ARC can evidently be turned off on a "per file" basis, perhaps allowing the use of legacy libraries... But I haven't played with it so I don't know yet. I'm pretty excited about it though. Can you imagine a world where iOS devs don't have to sweat retain/release?? What will we talk about here at SO?? ;-) – Dan Ray Jun 10 '11 at 15:31
  • 45
    Just when I finally got a good handle on all that memory management crap, and then they make it irrelevant. JOBS!!! – Kongress Jun 10 '11 at 15:35
  • @Dan: You're not joking it can be selectively turned off? Please give a link, that is important to me! :D – sudo rm -rf Jun 10 '11 at 15:43
  • This is behind the NDA wall, obviously, but it's mentioned at the top of the ARC guide document (right above "at a glance"): http://developer.apple.com/library/prerelease/ios/#documentation/General/Conceptual/ARCProgrammingGuide/Introduction.html#//apple_ref/doc/uid/TP40011029 – Dan Ray Jun 10 '11 at 15:53
  • @Dan: Thanks! I see it now, but the problem is that the “Migrate to Objective-C ARC” tool forces you to have all your files become ARC enabled. I guess that's where I got confused. Hopefully that can be changed in future betas. – sudo rm -rf Jun 11 '11 at 00:56
  • It think the point is that ARC will be the default. Seems sensible to me; if it works well, it takes a bunch of thinking off the developer. – Dan Ray Jun 12 '11 at 22:20
  • 1
    I believe the compiler isn't under NDA now so to selectively exclude some files (typically third party source folders) from your code, just add this as compiler option to each file: `-fno-objc-arc` – blackjack75 Jun 27 '11 at 02:24
  • @sudo rm -rf couldn't you just take the libs and remove all retain, release and autorelease stuff and change assign to weak and be done? Am I oversimplifying? – Dan Rosenstark Jul 18 '11 at 01:37
  • 2
    @Yar: Yes you are. I wish it was that simple, but unfortunately not all libraries are that simple. Take `JSONKit`, for example. Try running that through the ARC check. You'll see what I mean. ;) – sudo rm -rf Jul 18 '11 at 03:34
  • Apple recommends that ARC be used in all future projects. – Maciej Swic Jul 24 '11 at 23:45
10

iOS 5 is still under an NDA, and probably will be until they release the public version. If you have a developer account, head over to the Apple Developer Forums and ask there.

For previous versions, you have to count references and retain and release accordingly. Check out the Memory Management guide.

Edit: Here's a public spec for Automatic Reference Counting and a quote from the public iOS 5 page:

Automatic Reference Counting (ARC) for Objective-C makes memory management the job of the compiler. By enabling ARC with the new Apple LLVM compiler, you will never need to type retain or release again, dramatically simplifying the development process, while reducing crashes and memory leaks. The compiler has a complete understanding of your objects, and releases each object the instant it is no longer used, so apps run as fast as ever, with predictable, smooth performance.

nevan king
  • 112,709
  • 45
  • 203
  • 241
  • Will applications developed using iOS 5 work with older iPhones? – Geekgirl Jun 13 '11 at 14:50
  • You'll be able to use the tools to develop for older OSs, but you won't be able to use the new technologies like ARC. If you want to target older OSs, you'll have to do manual memory management. If you want to use ARC you'll have to restrict users to iOS 5. – nevan king Jun 13 '11 at 14:59
  • 1
    A reference to ARC is on a public facing Apple page http://developer.apple.com/technologies/ios5/ so at least some parts of it are not under NDA. – cobbal Jun 13 '11 at 15:54
  • 8
    Actually that's not quite true. You can build for iOS 4 with ARC. Quote from Apple engineer: "*For iOS 4 and Mac OS 10.6, the compiler adds a bit of runtime compatibility glue code to your app. This works for everything except __weak variables, which require more support than the compatibility code can provide. ARC on iOS 4 is simpler than non-ARC code, but it's not as simple as ARC on iOS 5.*" By the way, the WWDC schedule app was written with ARC and it worked on iOS 4 just fine! – sudo rm -rf Jun 14 '11 at 17:02
  • Thanks sudo, I didn't know it was possible. – nevan king Jun 15 '11 at 23:08
  • 3
    Yep; however that was qualified at the ARC talk as only 4.3.x targets get the 'compatibility glue'. – Alan Zeino Sep 19 '11 at 00:39
4

The details are light/under NDA at the moment, but Apple has implemented Automatic Reference Counting (ARC) in iOS 5, as detailed here: http://developer.apple.com/technologies/ios5/

If you develop a new app in Xcode 4 with the iOS 5 SDK, you can safely ignore retain/release counting.

[edit] sudo rm -rf makes a good point; third party libs may be significantly affected

Dominic
  • 3,304
  • 19
  • 22
  • Will applications developed using iOS 5 work with older iPhones? – Geekgirl Jun 13 '11 at 14:49
  • It will work on iPhones running iOS 5, so only the iPhone 3GS or iPhone 4. I don't believe it will support iOS 4, but then again, it's done by LLVM when compiling, so it might be possible to produce a binary for iOS 4 and 5. I highly recommend getting an iOS Developer account and playing around with the available options. – Dominic Jun 13 '11 at 15:53
  • As sudo points out in his comment on nevan's answer, you can indeed target back to iOS 4.0 with ARC, so older devices that can run that OS are compatible with this. – Brad Larson Jun 20 '11 at 21:50
3

It certainly is the choice of the developer or the team. ARC (Automatic Reference Counter) has made things a bit easier by automatically managing the memory for you. It will release, retain, and dealloc when appropriate. I do believe that you should gain experience managing the memory yourself preferably in a test application, if you haven't already. Another thing to consider is whether your application relies on third party libraries, which if not converted to ARC will prevent your application from compiling. The choice is obviously dependent on the situation at hand.

Charles
  • 39
  • 3
3

No one mentioned SystemConfiguration.framework? Please don't forget to put it into Frameworks. I miserably spent several hours to realize it.

ThinkChris
  • 1,169
  • 2
  • 15
  • 38
0

set flag as -fno-objc-arc in project settings>Build Phases > Compile Sources

Mubin Shaikh
  • 374
  • 4
  • 9