IOS5最佳实践(发布/保留?)

作为一个初学 iPhone 的程序员,编写应用程序使用 iOS5或更老版本的最佳实践是什么?具体来说,我应该继续使用数据的发布/保留,还是应该忽略它?这重要吗?

57617 次浏览

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.

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

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.

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.

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

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.

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