1

I see this example in the beginning of iOS development ,the chapter of picker view. And i don't understand why it use a strong reference here.

@property (strong, nonatomic) IBOutlet UIDatePicker *datePicker;

We always use weak property to reference the UI components.

Their superview will hold an array of subview.(hold the array of subviews, also hold each subview, Am I right?).

Therefore, I think we can just use a weak reference to reference the picker which is an subview of the main view. And the main view will hold the picker.

code4j
  • 4,208
  • 5
  • 34
  • 51
  • Don't rely on the superview for ownership of the control. If you had to purge the super view for any reason, you loose the control. Instead, allow the property to own it. See this link for more detail on the differences between weak and strong: [Differences between strong and weak in objective-c](http://stackoverflow.com/questions/11013587/differences-between-strong-and-weak-in-objective-c) – Jeremy Feb 07 '13 at 16:11
  • @Jeremy The answers are similar to my understanding, but I still don't understand why the book use the strong property. – code4j Feb 07 '13 at 16:15
  • @Jeremy oh, do you mean we don't want to loose the control in any even the parents are deallocated? – code4j Feb 07 '13 at 16:16
  • @Jeremy your comment conflicts with the link you have added. To clarify - if the superview is purged then there is no point in holding onto the subview (hence why weak is often used for non root views). You should not be holding data in your view's so it should not be an issue if the control is removed. – Paul.s Feb 07 '13 at 16:27

1 Answers1

3

Apple recommends that outlets should be declared as weak references.

I seem to recall that the advice used to be the opposite. If so, it seems likely that your example was written when the recommendation was to use strong (or retain, if it originally predated ARC).

(And I don't think this is a duplicate, since this question is specifically in reference to outlets and not about the fundamental difference between strong and weak.)

Steve Madsen
  • 13,465
  • 4
  • 49
  • 67