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

Why do we initialize?

So i'm in the Object Oriented Programming section of my Swift track but I still can't quite grasp why we need to initialize variables in our methods, i.e. you can't just write var hello:String. You have to then use an init method. Can anyone explain this to me?

Hi, i dont know but, you could re-watch the section like 3 times and carry on and you will eventualy understand. It works for me.

Unless somebody answer the question, you might get the answer but for time beeing you can re-watch it as you never know if sm1 will reply nd you dont want to waste time.

2 Answers

An analogy I usually like to use when programming Object Oriented is declaring a car. Suppose we have an object that is a car, we don't really know the details of the car. i.e. how many doors does the car have? how big is the car? what color is the car?

When you're initializing, you're telling the programming I want a "car object" with specific properties such as:

var color = UIColor.blueColor()
var numberOfDoors = 4
var carSize = "coop"

In swift there is another thing that happens in the background which is the allocation of memory. This tells the system, "I want this much storage space allocated for my application" depending on how big your class is. This storage space allocated aka memory is the space your application will be using to store the properties mentioned at the beginning of the post.

I hope I've help :)

but i'm still confused as to why you have to do:

self.color = color

I'm not sure if this may help you, but some languages call self "this". self and this refers to the current object. Using the analogy that I mentioned earlier:

class Car {
     var color = UIColor.blueColor()
     var numberOfDoors = 4
     var carSize: String

     // notice that the parameters name is the same as the properties name
     init(carSize: String) {
          // which one is the class property and which one is the parameter?
          carSize = carSize    // Warning: This will give a compilation error!

          // to fix this issue, you'll have to place self to distinguish which one is which
          // the self keyword is used to talk about the object itself 
          // object itself <- further explained outside this class if it doesn't make sense.
          self.carSize = carSize
     }
}

var maserati = Car()
// maserati in this case is the object being dealt with, and when working with this object inside of this class
// it is refered to as self. 
maserati.init("Sports Car")

if you still don't get it I have another example :)