4

Possible Duplicate:
Should IBOutlets be strong or weak under ARC?

I read about ARC briefly and thought ok, everything is strong and delegate is weak.

Now I'm creating a view in interface builder and making IBOutlets, and Xcode's default setting is set to weak.

There seems to be a reason for this suggestion, is there a reason most IBOutlets would want weak property?

Is that because these views(IBOutlets) are already retained because they are attached to its superview? and we seldom replace IBOutlet views?

But I don't see a harm in setting it as strong, is there a problem with it?

Community
  • 1
  • 1
eugene
  • 39,839
  • 68
  • 255
  • 489
  • Just make sure if you use strong, add `self.myOutlet = nil;` in the `viewDidUnload`. – Mazyod Aug 25 '12 at 04:58
  • Mazyod: would that be necessary? when self's retain count reaches zero, all its subviews wouldn't be released? – eugene Aug 25 '12 at 05:55

2 Answers2

5

For a discussion of why IBOutlet references should be weak, see the Resource Programming Guide: Nib Files. I quote from the guide:

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).

For my own two cents, I make things strong if I own it, or I need a strong reference in case the owner goes away and I need it to be retained, neither of which apply here. So, rather than asking, "why can't I make it strong?", I'd ask "why would I want to make it strong?" If there's no good reason to do so, I'd let Interface Builder do it's thing and make it weak.

Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • 1
    My two cents agree. I'd also add that it's easier to find cases where something's `weak` that should be `strong` ("Hey, I didn't expect that to be nil... I guess I need a strong reference to it") than the other way around (fire up Instruments and look for leaks). – rickster Aug 25 '12 at 05:50
  • @Rob: doesn't a view own its subview? or view controller own its view? I don't get "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." – eugene Aug 25 '12 at 05:56
  • 1
    @Eugene Yes, a view controller owns its view (hence "strong outlets are frequently specified by framework classes, for example `UIViewController`'s view outlet"), and a view owns its subviews. But a view controller does _not_ own its view's subviews; as you just pointed out, the view does. – Rob Aug 25 '12 at 11:03
0

It's just preference. The rationale for weak or assign is that the superviews, controllers, etc will hold strong references during the object's lifetime within that graph.

Personally, I am old school, and favor strong references -- even in this scenario. At times, using strong references means you may need to explicitly destruct components of your objects' subgraphs (e.g you must 'break' all circular dependencies when tearing down). I only use weak in very exceptional circumstances because I favor consistency; There are fewer variables at play when/if when something goes wrong and when reading programs.

justin
  • 104,054
  • 14
  • 179
  • 226