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?

1
  • 1
    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....
    – n13
    Commented May 4, 2013 at 8:21

7 Answers 7

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.

6
  • 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
    Commented Jun 24, 2011 at 4: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
    Commented Jun 24, 2011 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. (github.com/mirek/NSMutableDictionary-REST.framework)
    – casey
    Commented Jun 25, 2011 at 0: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
    Commented Jun 25, 2011 at 0: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. Commented Apr 6, 2012 at 18:46
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.

10
  • 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
    Commented Jun 10, 2011 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
    Commented Jun 10, 2011 at 15:35
  • @Dan: You're not joking it can be selectively turned off? Please give a link, that is important to me! :D Commented Jun 10, 2011 at 15:43
  • 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 Commented Jun 27, 2011 at 2:24
  • 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. ;) Commented Jul 18, 2011 at 3:34
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.

6
  • Will applications developed using iOS 5 work with older iPhones?
    – Geekgirl
    Commented Jun 13, 2011 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
    Commented Jun 13, 2011 at 14:59
  • 1
    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.
    – cobbal
    Commented Jun 13, 2011 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! Commented Jun 14, 2011 at 17:02
  • 3
    Yep; however that was qualified at the ARC talk as only 4.3.x targets get the 'compatibility glue'.
    – Alan Zeino
    Commented Sep 19, 2011 at 0: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

3
  • Will applications developed using iOS 5 work with older iPhones?
    – Geekgirl
    Commented Jun 13, 2011 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
    Commented Jun 13, 2011 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. Commented Jun 20, 2011 at 21:50
3

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

1
  • you should explain why. Commented Oct 10, 2013 at 15:54
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.

0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.