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
Rodrigo Chousal
16,009 PointsWeak and Strong attributes
In which case would you want to use a weak attribute? Why wouldn't you want memory to be allocated for something in your file? (I know there is a video, but the matter is still unclear to me).
3 Answers
Stone Preston
42,016 PointsBasically, you want to use weak when that object is part of a parent-child relationship. For example, a text field is a child of the view controller (the parent. The text field is declared with a weak attribute. The view controller itself has a strong reference to the text field, but the text field has a weak reference going back to the view controller. If they were both strong, you would have whats called a retain cycle. You can read more on strong vs weak and other ARC stuff here.
Most of the time you are going to be using strong, but sometimes (as is the case with controls like text fields, buttons, etc that BELONG to a view controller) you use weak
Rodrigo Chousal
16,009 PointsThis must be fairly simple as well, but I never quite understood it. What is an instance? What is a class? What is a method? What is a function? What about a property? It would really help me to know, in a nutshell what each of these are and do...
Stone Preston
42,016 Pointsan object is an instance of a class. A class is like a blueprint of an object, an instance is like the realization of that class. Say you had a Dog class, that class would lay out what a dog needs to look like and do. An instance of that class (aka object) would be the actual dog.
A method is pretty much a function that belongs to a class that you can reference (known as calling a method) to execute some code. Methods can do anything really. You can have instance methods which are called on instances of classes (so you need to have an object to use them) and class methods (they dont require an object to use them). The dog class could have an instance method called sayName which would log the dog objects name variable to the console. It could have a class method called makeDog which would instantiate a new dog object
objects are kind of hard to conceptualize and are pretty abstract ideas which makes it hard to see their usefulness. But after using them for a while you understand a bit better.
Rodrigo Chousal
16,009 PointsDo you need a class for every object?
Fernando Claussen
9,324 PointsNo, the idea of using a class is to reuse it.
Lets say I have the dog class that have some info about the dog. His fur type, nose type, breed, etc.. And some methods, let's say, bark()
I can instantiate as many objects as I want with this class.
I can say
Dog *rex = [[Dog alloc] init];
Dog *lola = [[Dog alloc] init];
and call different methods and assign different characteristics to each one of them.
Rodrigo Chousal
16,009 PointsAlright...Thanks.
Rodrigo Chousal
16,009 PointsRodrigo Chousal
16,009 PointsI understand that, but my doubt is about memory allocation and the role of weak and strong. I keep hearing that when we use weak "we don't care whether there is memory stored for that, and the system can at any time deallocate it (I'm not sure what the thing you use it on is called)".
Stone Preston
42,016 PointsStone Preston
42,016 Pointsthe view controller's (the parent) strong reference to the child will keep it in memory as long as the parent is alive. if the text field was strong as well, the view controller could be deallocated and the text field would continue to exist in memory. The article I linked will probably clear it up for you
Rodrigo Chousal
16,009 PointsRodrigo Chousal
16,009 PointsOooooh... That clears my doubts. I can't believe it's that simple... Thank you so much!