IBOutlet and viewDidUnload under ARC

前端 未结 5 787
野性不改
野性不改 2021-01-30 09:01

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

相关标签:
5条回答
  • 2021-01-30 09:39

    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

    0 讨论(0)
  • 2021-01-30 09:41

    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.

    0 讨论(0)
  • 2021-01-30 09:47

    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.

    0 讨论(0)
  • 2021-01-30 09:48

    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.

    0 讨论(0)
  • 2021-01-30 09:50

    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.

    0 讨论(0)
提交回复
热议问题