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

Weary

I've been somewhat frustrated with the failure of the iOS course to address basic concepts in Objective-C. For instance, the lesson on Memory Management seems as if it deserves an introduction like this, which I found on another site:

The goal of a memory management system is to make sure that programs don't take up any more space than they need to by creating and destroying objects in an efficient manner.

Many languages accomplish this through garbage collection, but Objective-C uses a more efficient alternative called object ownership. When you start interacting with an object, you’re said to own that object, which means that it’s guaranteed to exist as long as you're using it. When you're done with it, you relinquish ownership, and—if the object has no other owners—the operating system destroys the object and frees up the underlying memory.

I know it's not hard to find on your own, but still, it would be better if Treehouse's concept summaries were better (or existed at all) because they relate to the lesson examples.

And sometimes, concept summaries refer to a lesson but don't quite match up with what's in the lesson. I'm having a hard time understanding strong and weak properties, so I'm reviewing the material. The Memory Management lesson (kind of) refers to the Inheritance lesson and cites the following code. But this code was never actually in the lesson.

@interface Shape : NSObject
@property(nonatomic, weak) Button *button
@end 

Am I missing something here? Is this a problem for anyone else?

There's also non sequiturs: take the Memory Management lesson for example: it begins with a reference to 'malloc' and 'free'. What? These have never been mentioned in the course before, and they're never explained in this lesson.

Edit in case anyone from Treehouse sees this ... Amit Bijlani

Having now completed the Beyond the Basics course, I see that a lot of these concepts are explained within this module. Given, Amit is a great presenter and the informal, demonstrative style of these lessons makes them much more comprehensible.

But I can't help wondering if some content would be better introduced earlier on. Like, the whole memory-management thing is skirted around until the end and this really confused me – might just be an individual learning-style thing though.

3 Answers

I'll start with the strong and weak, so abive u said that if you're using the object it will remain in storage but ince u stop using it it's gone. Well every object has a reference count and as long as it's not NULL or 0 then the object is still in memory, so for in stace if i did

NSString *demo = @"hello";

//then i point to the object

NSString *demos = [[NSString alloc] init];

demos = demo;

So in that i made a string called demo that holds a string, so when i do that there is memory made for demo and it's reference count is now at 1. Then i use demos to point to demo so now demo's count is 2 because when it's allocated it gains a count and when demos points to it, it gains a count. Now if your object has 0 or null as a value it will deallocate which is something we dont have to worry about because ARC (automatic reference count) will keep track of this and handle the deletion of the object, but say we dont want something to be deallocated? Well we can give it a strong propertie which will keeps it's count at a minimam of 1 meaning it cant be deallocated but by default objects are of type weak meaning that it will be deallocated from memory once it's been used and has a reference count of 0. So in your code that u put in your question he makes a button that is nonatomic (which is used if you're not threading which is very complex) then weak meaning it can be destroyed if it get a reference count of 0, then its of type button named button.

Hope this helps and if u want to learn more u can u can always check the apple documentation which is a great place to learn.

-Kai.

Thanks Kai this has helped

No problem :), best of hopes for our iOS

Kai

Amit Bijlani
STAFF
Amit Bijlani
Treehouse Guest Teacher

Thank you for your feedback. It always helps to understand how effective our courses have been for our students. I'm always careful to introduce complex topics upfront because it overwhelms students. A lot of what you learned in Objective-C Basics is reinforced in the courses that come after it. I understand that at this point some of the concepts seem nebulous, however when you see some them used in an actual app it will help give you some context. Not to mention seeing the same concepts applied in various scenarios also helps. Learning something new can be daunting and it's great that you are seeking answers to your questions. My only advice is to practice what you have learned on your own. You can watch and read all you want but unless you apply it yourself the brain does not make the connection.