Ios – IBOutlet and viewDidUnload under ARC

automatic-ref-countingiboutletiosweak-references

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];
}

Best Answer

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