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 and when should we use an initializer?

Dear all,

I am a beginner to programming and currently studying with the course SWIFT 2.0 Orientation bla bla bla, and Pasan is teaching us to use initializer(init) in "Struct"

However, I am not very understand Why and When I should use an initializer (init) in my coding Struct. (e.g. why we use "self.x = x" can't we use "self.x = abc" ?)

Any impact if I not to use initializer? or I can do something else to replace this initializer?

Hope someone can help me, many thanks and have a nice day.

3 Answers

I'm a beginner as well, so someone correct me if i'm wrong.

the init() is a function/method. It is used to do things, when in your source code an instance of a class (or struct as well?) is created. Not sure how much you know about classes and creating instances of classes. If you don't, wait until you have that and you might understand. If yes: creating an instance of e.g. a class will call this method.

init() on a beginner level is mainly used to initialise the variables. You are not setting hard values here, but say "hey user, when you create on object of me (the class) I want you to initialise the parameters (means setting the values), that I have declared in my init-method". This "forces" the user to set values.

The assignment in init() is a bit confusing. The variable right to the = is a parameter. This parameter has to be set during creation of a class object. This is then assigned to the class' property, that is on the left of the = sign. You can e.g. write "self.x = x" or "xProperty = xParameter" or "x = _x". See the example below:

class Person {
   //Properties (synonyme for variables in a class)
   var name: String
   var age: Int

   init(inputName: String, inputAge: Int) {
      name = inputName
      age = inputAge
   }
}

Can we leave this out? Yes, init() is not required. BUT, then we 1. create a class instance that does not ask for parameters, which might be a problem when we want to set them and 2. we need to initialise the properties right at the moment of the declaration. So it needs to be 'var name: String = "initialName" ' instead of 'var name: String'.

I'm yet a newbie, but when creating a class, I for now always create the init-method, until I know better.

thank you Sorgalla, I understand a bit more about initializer after read your reply. But can you elaborate more about the point two you mentioned at last? any example shows what will be happened if we just set var name: String ?

many thanks.

Hey Kanix, write down below code into Xcode, no matter if in a project or a playground. See, what happens.

class Person {

   var name: String
   var age: Int

}

Xcode will output a compiler error. If you check the error, it tells you that the variables which you declared, are not initialized yet. The compiler even offers 2 actions it can do to get rid of the errors. Click the 2 "fix-it" when selecting the error in the left gutter and see what it does.

As we deleted the init method now but kept the rest the same, xcode can't find an initial value for your variables anymore. So you need to initialize them now with the other option that is available to set initial values, that is by writing them in the same line where you declared the variables.

Is there anything unclear yet after you tried the above code by yourself?

Hello Sorgalla,

Thanks so much for your advice, it was really helpful :)

Regards Kanix

Glad I could help man :)