Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialAlex Atwater
2,624 PointsStrong vs Weak storage
Can someone please help me? This is driving me nuts. I have a pretty good basic understanding of classes and everything iPhone, but the one thing that really bothers me and is hindering my progress from being a βnoobβ to good, is the difference between strong and weak storage types. Can someone please explain them? Like how, why, when to use which one and an example? Just whatever, i really donβt understand and will randomly choose one when it asks meβ¦ Thank in advance
1 Answer
eirikvaa
18,015 PointsWhen you declare that a property should be strong, you are saying that "keep this object in the heap as long as I point to it strongly". When you use the weak storage, you are saying that "keep this object in the heap as long as someone else points to it strongly".
A good example is the parent-child scenario. Let's say that you have a UILabel on a blank scene. The scene (or the view) has a strong pointer to the UILabel and the UILabel has a weak pointer to the scene. Because of the strong pointer from the view to the label, as long as the view is alive, the label is alive. If you switch scenes, the previous scene will be deallocated and since the view is no longer alive, the label is also deallocated.
If there is a parent-child relationship where both the parent and the child has a strong pointer to the other, they will never be deallocated from the heap (because you said that both should be kept alive) and thus cause a memory leakage. You don't want that.
It's a good question, not a noob question at all. Hope this clears things up for you.
Tommy Choe
38,156 PointsTommy Choe
38,156 PointsI know you answered this post a year ago but it's cleared things up for me. Thank you!