iOS 5 Best Practice (Release/retain?)

57,602

Solution 1

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.

Solution 2

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.

Solution 3

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.

Solution 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

Solution 5

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

Share:
57,602

Related videos on Youtube

Geekgirl
Author by

Geekgirl

Live well, laugh often, love much.

Updated on July 08, 2022

Comments

  • Geekgirl
    Geekgirl almost 2 years

    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?

    • n13
      n13 almost 11 years
      Use ARC, and follow the best practices as outlined here: 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....
  • Dan Ray
    Dan Ray almost 13 years
    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?? ;-)
  • Kongress
    Kongress almost 13 years
    Just when I finally got a good handle on all that memory management crap, and then they make it irrelevant. JOBS!!!
  • sudo rm -rf
    sudo rm -rf almost 13 years
    @Dan: You're not joking it can be selectively turned off? Please give a link, that is important to me! :D
  • Dan Ray
    Dan Ray almost 13 years
    This is behind the NDA wall, obviously, but it's mentioned at the top of the ARC guide document (right above "at a glance"): developer.apple.com/library/prerelease/ios/#documentation/…
  • sudo rm -rf
    sudo rm -rf almost 13 years
    @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.
  • Dan Ray
    Dan Ray almost 13 years
    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.
  • Geekgirl
    Geekgirl almost 13 years
    Will applications developed using iOS 5 work with older iPhones?
  • Geekgirl
    Geekgirl almost 13 years
    Will applications developed using iOS 5 work with older iPhones?
  • nevan king
    nevan king almost 13 years
    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.
  • Dominic
    Dominic almost 13 years
    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.
  • cobbal
    cobbal almost 13 years
    A reference to ARC is on a public facing Apple page developer.apple.com/technologies/ios5 so at least some parts of it are not under NDA.
  • sudo rm -rf
    sudo rm -rf almost 13 years
    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!
  • nevan king
    nevan king almost 13 years
    Thanks sudo, I didn't know it was possible.
  • Brad Larson
    Brad Larson almost 13 years
    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.
  • casey
    casey almost 13 years
    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?
  • Deamon
    Deamon almost 13 years
    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.
  • casey
    casey almost 13 years
    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. (github.com/mirek/NSMutableDictionary-REST.framework)
  • casey
    casey almost 13 years
    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!
  • blackjack75
    blackjack75 almost 13 years
    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
  • Dan Rosenstark
    Dan Rosenstark almost 13 years
    @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?
  • sudo rm -rf
    sudo rm -rf almost 13 years
    @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. ;)
  • Maciej Swic
    Maciej Swic almost 13 years
    Apple recommends that ARC be used in all future projects.
  • Alan Zeino
    Alan Zeino over 12 years
    Yep; however that was qualified at the ARC talk as only 4.3.x targets get the 'compatibility glue'.
  • Gopalakrishnan Subramanian
    Gopalakrishnan Subramanian about 12 years
    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.
  • Duck
    Duck almost 12 years
    I love how intuitive Xcode 4.x is. Just press enter to type the flags. Amazing. Thanks Apple.
  • John Riselvato
    John Riselvato over 10 years
    you should explain why.