Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IBOutlet and viewDidUnload under ARC

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 - in fact any reference to a "parent" object, to be a good citizen and think about the object graph for a minute - should be zeroing weak references. Due to the nature of zeroing weak pointers automatically dropping to nil on the referenced object's retain count reaching zero, does this mean that setting IBOutlets to nil in viewDidUnload is now unnecessary?

So, if I declare my outlet like so:

@property (nonatomic, weak) IBOutlet UILabel *myLabel; 

Does the following code have any effect?

- (void)viewDidUnload {     self.myLabel = nil;      [super viewDidUnload]; } 
like image 233
Stuart Avatar asked Oct 07 '11 01:10

Stuart


2 Answers

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

like image 125
Daryl Teo Avatar answered Sep 21 '22 22:09

Daryl Teo


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.

like image 30
Stuart Avatar answered Sep 17 '22 22:09

Stuart