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 trial

iOS Objective-C Basics (Retired) Beyond the Basics Property Attributes

@property strong vs weak

So, I get the retain count. to my understanding is just something that keeps track of the memory allocated for a variable. If I create a NSString with a strong attribute I know my retain count goes up to one. But realistically speaking what does this mean? Can people expand on this idea

Thanks

2 Answers

Mike Baxter
Mike Baxter
4,442 Points

You'll probably save yourself a lot of headache if you avoid thinking in terms of retain count. The "strong" and "weak" memory types are really meant to help you avoid thinking about retain count. Instead, you should think in terms of ownership, of strong and weak.

A strong memory reference means ownership of an object.

A weak memory reference means no ownership of an object.

And you have a big problem when two objects own one another. How can they own one another? If they each have a strong reference to one another, then they are said to own one another. (This is called a "strong retain cycle", or "retain cycle" for short.) It means that neither of the objects can ever get deleted because ARC thinks they still have an ownerβ€”and we know that only things without owners get deleted. You're perfectly fine having as many things as you'd like be owners of an object (in other words, to have a strong reference to an object), but you shouldn't ever have a case where you have a retain cycle.

Thank you!