iOS 5 Best Practice (Release/retain?)

IphoneIosIos5Memory ManagementAutomatic Ref-Counting

Iphone Problem Overview


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?

Iphone Solutions


Solution 1 - Iphone

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 2 - Iphone

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 3 - Iphone

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

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

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

Solution 6 - Iphone

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.

Solution 7 - Iphone

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGeekgirlView Question on Stackoverflow
Solution 1 - IphonepixelfreakView Answer on Stackoverflow
Solution 2 - Iphonesudo rm -rfView Answer on Stackoverflow
Solution 3 - Iphonenevan kingView Answer on Stackoverflow
Solution 4 - IphoneDominicView Answer on Stackoverflow
Solution 5 - IphoneThinkChrisView Answer on Stackoverflow
Solution 6 - IphoneCharlesView Answer on Stackoverflow
Solution 7 - IphoneMubin ShaikhView Answer on Stackoverflow