Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS

Nic Huang
PLUS
Nic Huang
Courses Plus Student 10,573 Points

property use, strong or weak ???

I just finished the second video "Creating a Custom Class - Part 2" in building BlogReader. The video states that we should use property "strong" in "parent to child relationship", and "weak" in "child to parent relationship" when we are creating property.

But how do you define "parent to child relationship" and "child to parent relationship"??

any good example ???

appreciate your help.

2 Answers

Amit Bijlani
STAFF
Amit Bijlani
Treehouse Guest Teacher

The easiest way to explain is by way of an example. When you create a UITableViewController you have a main property pointing to the tableView which would be of type strong. However, the tableView has two property delegate and datasource both of which are protocols that define methods. However, these methods are defined in the tableView's parent class which is the table view controller.

Here's a diagram illustrating the above example:

Nic Huang
Nic Huang
Courses Plus Student 10,573 Points

Thank for your explanation. Really helpful!! It sounds like table view controller is the grandpa of delegate and datasource property. That's why they are weak. I'm not sure if I get this right.

But let's say if I randomly create an UIButton and UITextView on storyboard and want to use property to manipulate them, what property should I give them, weak or strong ?? Because I've seen both.

Amit Bijlani
Amit Bijlani
Treehouse Guest Teacher

In your case of adding a control, think of it this way, the view controller is the parent class and the properties are its children. So yes, you would use the strong attribute for the properties. However, a UITextView has a delegate property which would be implemented by the view controller so in that case the delegate property would be weak because the UITextView is a child of the view controller.

Nic Huang
Nic Huang
Courses Plus Student 10,573 Points

I just watched ARC deep dive video. it talks about UITextView. Let me put this way to see if I really understand this.

I could use strong attribute for my UITextView property because it's a property of view controller, parent class. And UITextView has a weak-attributed delegate. When the view controller is gone, this delegate automatically goes away with UITextView ( Strong ) held by the view controller. So there is no point for view controller holding onto the delegate, that's why it is weak. ViewController is the center.

Hope I'm right.

In the simplest of cases you use a strong relationship to keep something alive, and a weak relationship when something (that is being kept alive by something else) needs to refer back to what is keeping it alive.

A view controller has a view, that view is being kept alive by the view controller (the view controller has a strong relationship to it) but the view may want to communicate with its view controller. So that view could have a weak relationship to the view controller.

Nic Huang
Nic Huang
Courses Plus Student 10,573 Points

Thanks for you explanation. Looks like property involves different perspectives.

Because I am always wondering which property ( weak or strong ) I should assign when I create an IBOutlet or variable, and what would happen then if I assign improper property. I just can't see any difference