Should IBOutlets be weak or strong?

You might have noticed that when you insert an IBOutlet directly from Interface Builder you can select whether to have it as a weak or strong property. After a little search in the Apple developer’s library I have found when you should use one or the other:

Outlets should generally be weak, except for those from File’s Owner to top-level objects in a nib file (or, in iOS, a storyboard scene) which should be strong. Outlets that you create should therefore typically be weak, because:

 

  • Outlets that you create to subviews of a view controller’s view or a window controller’s window, for example, are arbitrary references between objects that do not imply ownership.
  • The strong outlets are frequently specified by framework classes (for example, UIViewController’s view outlet, or NSWindowController’s window outlet).

You can read more about it here.

3 Replies to “Should IBOutlets be weak or strong?”

  1. I have often wondered this. Nice find, Vasilica.

    Though that does beg the question, if you are supposed to do everything but top-level objects in weak, why even give us the option in the IBOutlet wizard, to help further enforce that. It’s not like we can’t change the code manually afterwards anyway.

    Just as a note of additional information, NSHipster did a post about it at:
    http://nshipster.com/ibaction-iboutlet-iboutletcollection/
    It sounds like he agrees except when it is necessary. He points out something else from the same Apple article:

    “You may in some situations need an object from a nib file to exist outside of its original container. For example, you might have an outlet for a view that can be temporarily removed from its initial view hierarchy and must therefore be maintained independently.”

    And he further notes that using strong too much may get into problems with retain cycles, so pretty much in agreement of using weak most of the time.

    • I think your post is really interesting. However, I am not convinced that outlets should be strong. As you pointed out the Apple docs still suggests that unless you have a good reason for it, outlets should be weak. Can you point out which WWDC session was it where they talked about this?

Leave a Reply

Your email address will not be published. Required fields are marked *

*