There is a similar question to this on SO here, however I just want to clarify something that wasn\'t fully explained there.
I understand that all delegates and outlets
Just doing a bit of research...
As I understand it, weak is similar to assign, in that they're both weak references.
However, assign does not create a zeroing reference. i.e. if the object in question is destroyed, and you access that property, you WILL get a BAD_ACCESS_EXCEPTION
.
Weak properties are automatically zeroed (= nil) when the object it is referencing is destroyed.
In both cases, it is not necessary to set property to nil, as it does not contribute to the retain count of the object in question. It is necessary when using retain properties.
Apparently, ARC also introduces a new "strong" property, which is the same as "retain"?
Research done here
I did a little testing and it appears that the code in the viewDidUnload
method is unnecessary. To support this, the docs for viewDidUnload
do actually say:
By the time this method is called, the view property is nil.
Indicating that the weak reference must have been set to nil
automatically.
I have some empirical evidence to support that IBOutlets are indeed already set to nil automatically. Here's what I did:
@synthesize myLabel = myLabel_
) so that I may later inspect their values in the debugger.viewDidUnload
.viewDidUnload
to get called by simulating a memory warning.The explicit ivars all had nil
as their value then I hit the breakpoint.
From my understanding of how the outlets are managed in ARC if you are using a weak reference, you do not need to add anything to viewDidUnload as it will already be nil. Doing so is thus redundant.
However if you do have strong outlets, which apple says you should do if you are pointing to a top level item in the nib, then you should definitely continue to add the appropriate line in viewDidUnload to nil out these ones.
Starting from iOS 5 and OS X 10.7, weak
will produce an automatic zeroing pointer. This means that when the pointed object is release, the pointer is automagically set to nil
(for details see Zeroing Weak References in ARC).
So, under iOS 5+ and OS X 10.7+, it is not useful to set the weak
IBOutlet
properties to nil
manually in the viewDidUnload
method: when the main view is unloaded, all its subviews will be released, so the related properties are set to nil
.