IBOutlet and viewDidUnload under ARC

最后都变了- 提交于 2019-12-20 08:37:46

问题


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

回答1:


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




回答2:


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.




回答3:


I have some empirical evidence to support that IBOutlets are indeed already set to nil automatically. Here's what I did:

  1. I set up explicit ivars for my IBOutlet properties (@synthesize myLabel = myLabel_) so that I may later inspect their values in the debugger.
  2. I enabled a breakpoint on the first line of viewDidUnload.
  3. I arranged for viewDidUnload to get called by simulating a memory warning.
  4. I inspected the values of the explicit ivars that I associated with my IBOutlet properties.

The explicit ivars all had nil as their value then I hit the breakpoint.




回答4:


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.




回答5:


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.



来源:https://stackoverflow.com/questions/7682322/iboutlet-and-viewdidunload-under-arc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!