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) Advanced Objective-C Memory Management

At 2:16 of this video, I started wondering: What's with the (nonatomic, strong)?

I noticed these attributes and wondered what they mean.

2 Answers

Nick Sint
Nick Sint
4,208 Points

The "nonatomic" declaration can be a bit confusing but at this early stage in learning, just remember that all your properties should be nonatomic.

The "strong" pointer is a lot more important to help build your foundation in object oriented language. Almost everything you create is an "object" (i.e. strings, images, views) that gets stored in memory. However, these "objects" only exist in memory if something is "pointing" to them (i.e. using them). For example, you can create the string "Hello World!" and this string would only actually remain in existence if you create a pointer to it. In other words, everything that is not in use, is automatically destroyed because it would be a waste of space to keep it around.

There are 2 types of pointers that you can have; "strong" vs "weak". If a pointer to an object is "weak", this implies that this object will only remain in existence if there is another pointer TO THE SAME OBJECT that is "strong". If a pointer to an object is "strong", this implies that this object will continue to remain in existence until that pointer (and all other "strong" pointers to the object if any other exists) are destroyed.

Therefore it is important to declare your variables correctly either as strong or weak. Which to pick will depend on when you need the object to exist and when you want it to get destroyed.

Sandro Meschiari
Sandro Meschiari
18,418 Points

Great explanation better then the video!!!

Russell Beye
Russell Beye
3,367 Points

Nonatomic has to do with how the property will behave in a threaded environment. If you are running multiple threads, you want to ensure that the getter and setter aren't run at the same time. They would interrupt each other and could cause possible corrupted data.

Properties default to atomic states, which does cost some overhead but remain thread-safe. Since the tutorials aren't really going over multithreaded environments (I think, haven't done them yet), they use a non-atomic (bit faster and less overhead, but not thread-safe).

Strong is required for an attribute that is a pointer to an object. It basically keeps it in the autorelease pool.

If this sounds like gibberish, it's exactly why they didn't go over... At least just yet. :)