1
votes

When using ARC with iOS 5, a weak IBOutlet creates a zeroing reference, avoiding the need to

self.< IBOutlet property > = nil;

in -(void)viewDidUnload

If I'm using iOS 4 (and using ARC) and forced to use unsafe_unretained instead, does it mean I have to override viewDidUnload and set the property to nil manually?

EDIT: This relates to my case: Should IBOutlets be strong or weak under ARC? The exception being: I can't use the 'weak' keyword which creates the zeroing reference.

Hope my question is clearer.

Thanks

2
WHy are you using unsafe_unretained? In iOS (as opposed to OS X) IBOutlets should be retained.zaph
@Zaph I've just had a look at the Resource Programming Guide - Managing the Lifetimes of Objects from Nib Files and it seems to suggest you only use strong for the top level objects and weak for everything else, which is contrary to what I used to believe but it makes sense with this line Outlets that you create to, for example, subviews of a view controller’s view or a window controller’s window, are arbitrary references between objects that do not imply ownership..Paul.s
@Paul I understand and what you say is true--but that advice is really only good for developers that fully understand all of the implications. Clearly this op doesn't and that applies to a great many developers. Please see my answer below.zaph

2 Answers

3
votes

When using ARC, as I am sure you have realized, the weak attribute cannot be used pre-iOS5. The other side of that coin would be to use unsafe_unretained. Weak attributes will automatically set your properties to nil. Unsafe_retained (aka "assign" in pre-iOS 5), will not, and you need to do this yourself.

1
votes

Without a property (in iOS) the IBOutlet will be ivar is set and retained by KVC. With a @property the ivar is set by setting the property.

On an ARC project, if one creates a nib and drags an item (say UILabel) to the .h file a strong @property will be added as well as in the .m file a line setting the property to nil will be added to the viewDidUnload method and a @synthesize statement for the property.

There are other ways to handle the retaining of nib IBOutlets that work and may even be better by some metric.

From the Apple document Resource Programming Guide - Managing the Lifetimes of Objects from Nib Files:

Because the behavior of outlets depends on the platform, the actual declaration differs:

For iOS, you should use:
@property (nonatomic, retain) IBOutlet UserInterfaceElementClass *anOutlet;

For OS X, you should use:
@property (assign) IBOutlet UserInterfaceElementClass *anOutlet;

My belief is to not fight the way Apple does things, doing so tends to make things harder. Also consider that Apple has inside information of the future of the platform. :-)