iOS 5 Best Practice (Release/retain?)
Asked Answered
L

7

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?

Leomaleon answered 10/6, 2011 at 15:19 Comment(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....Scurlock
R
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.

Renteria answered 10/6, 2011 at 15:28 Comment(10)
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?? ;-)Archicarp
Just when I finally got a good handle on all that memory management crap, and then they make it irrelevant. JOBS!!!Sachs
@Dan: You're not joking it can be selectively turned off? Please give a link, that is important to me! :DRenteria
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/…Archicarp
@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.Renteria
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.Archicarp
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-arcFerromagnesian
@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?Mose
@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. ;)Renteria
Apple recommends that ARC be used in all future projects.Wrench
C
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.

Calceolaria answered 21/6, 2011 at 18:5 Comment(6)
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?Williamwilliams
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.Calceolaria
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)Williamwilliams
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!Williamwilliams
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.Boeke
I love how intuitive Xcode 4.x is. Just press enter to type the flags. Amazing. Thanks Apple.Meliorism
R
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.

Renteria answered 10/6, 2011 at 15:28 Comment(10)
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?? ;-)Archicarp
Just when I finally got a good handle on all that memory management crap, and then they make it irrelevant. JOBS!!!Sachs
@Dan: You're not joking it can be selectively turned off? Please give a link, that is important to me! :DRenteria
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/…Archicarp
@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.Renteria
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.Archicarp
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-arcFerromagnesian
@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?Mose
@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. ;)Renteria
Apple recommends that ARC be used in all future projects.Wrench
B
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.

Bounds answered 10/6, 2011 at 15:23 Comment(6)
Will applications developed using iOS 5 work with older iPhones?Leomaleon
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.Bounds
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.Palladin
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!Renteria
Thanks sudo, I didn't know it was possible.Bounds
Yep; however that was qualified at the ARC talk as only 4.3.x targets get the 'compatibility glue'.Perth
C
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

Coy answered 10/6, 2011 at 15:23 Comment(3)
Will applications developed using iOS 5 work with older iPhones?Leomaleon
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.Coy
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.Else
G
3

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

Goral answered 10/1, 2012 at 1:7 Comment(1)
you should explain why.Oscillation
H
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.

Haug answered 29/11, 2012 at 19:51 Comment(0)
Q
0

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

Quintuplet answered 20/5, 2014 at 14:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.