Sunday, June 3, 2012

iOS 5 Best Practice (Release/retain?)


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?



Source: Tips4all

5 comments:

  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.




    See the full transition guide here.

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


    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 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.

    ReplyDelete
  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.

    ReplyDelete
  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

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

    ReplyDelete