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 trialMike Lange
794 PointsUnderstanding allocation & initialisation
I can't completely wrap my head around the concept of allocation & initialisation.
ie. We have in this example:
Shape *shape = [[Shape alloc] init]
Q1. Does *shape have any relevance to the object "Shape", or would this work just the same as any other name eg.
Shape *subshape = [[Shape alloc] init]
Q2. What are the rules behind allocation and initialisiation - is this a standard requirement to add this line before you can utilise any object files and their associated functions?
1 Answer
Manuel Schulze
12,739 PointsHey Mike,
I hope that I can help you with your problem and I hope i get it right.
Let's start with Q1: You can use any other name.
"Shape" is the class or the plan how to build an object of type "Shape". And "shape" is the concrete object. Using another example. "Car *car = ....";. "Car" is the class and there you write down things like every car can drive (or has a method/function drive) and every car has a color. But you don't know which color so you can initialize it with yellow e.g. You can now create Car *auto = ... or Car *circle = ... So you can use every name you want to and it will work. This is necessary because you'll need more than one object of one type very often in programming.
Q2:
I think it will help you to understand that statement if I split it and explain it one by one.
Shape *subshape = [[Shape alloc] init]
The first part (Shape *subshape) means this is a variable of the type "Shape" and with the name subshape (forget about the * this is a whole new story). But now you don't have an object only a variable so you need to give it a value. It's the same like "int i = 4;" in C.
But if you want to create an object you need two steps. First you need to make free memory for it on your device and initialize it with values. So you allocate it first with [Shape alloc] - this means you allocate memory for an object of type Shape (which is you class or your plan how to build concrete objects as mentioned before).
Now you have the memory but you didn't initialize the values. This is important let's think of the car again. A car object may have a variable wheels. And all cars have 4 wheels so every car object that you create will have the value 4 after you initialized it. To make this clear I'll give you another example. The car has a variable colors. But not all car has the color yellow. So you can use another initializing method than init where you can pass the color too.
I don't know exactly about the syntax of Objective-C but it should look like this: Car *myCar = [[Car alloc] initWithColor: @"blue"];
So you have to allocate the memory first and then you can initialize the concrete object. And for sure before you can use an object you have to do both - give it some memory and initialize its values. If you only do this step: Car *myCar; you can't use it because it's just a variable that can save the place where the car object is but it's no concrete object with values etc.
I hope this doesn't make it worse and answered your questions.
Greetings Manu
Mike Lange
794 PointsMike Lange
794 PointsThanks Manu, it helps! More questions arise from your answers but I'm sure these will clear up as I get further into the course and get familiar with all the methods.