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


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!