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

Mark Pittam
1,084 PointsWhy use init() when initial values are known?
I'm not sure I understand what the point of an init() method if the initial values are already known before initialization. Perhaps someone could explain what the difference is between:
class Runner {
var speed = 6
}
and:
class Runner {
var speed: Int
init() {
speed = 6
}
}
Seems like a few extra lines of typing to get the same result... I had thought it might be that init() was required for the class to compile, but it seems the first bit of code works just fine...
2 Answers

Chris Shaw
26,676 PointsHi Mark,
I think you're misunderstood as to how and why classes/structs work in Swift and common programming languages alike, let's break it down.
- An
class
and/orstruct
are designed to be reusable therefore they shouldn't carry static business logic - they should be abstract and offer no relationships to other parts of your code
- they should never be self destructive, only the code that created the instance should be allowed to destroy that instance
What does all of that mean?
Let's start with an simple example, say we have a String
literal and assign it to a variable called myString
, we have just created a new instance of the String
type but we can also use the constructor which uses the init
method to pass a string in, for example.
Literal instantiation
var myString: String = "This is my string"
// myString equals "This is my string"
Non-Literal instantiation
var myString: String = String("This is my string")
// myString equals "This is my string"
In both examples the literal and non-literal are exactly the same, the only difference is we called the init
constructor in the second example which takes one argument by default and that's a string literal.
Confusing right?
Don't worry, I used this simply as an example of why the init
method is important, while in the end the first example would suffice we always have the opportunity to access the constructor even though we don't need it to begin with but the common understanding is we get back the same string and not anything unexpected.
Let's try something else using the type String
, the the following.
var myString: String = ""
// myString equals ""
var myString: String = String()
// myString equals ""
Again we're using the init
constructor by using String()
but this time our result is empty since all values of type String
by default have no value which is defined in the class
in the class itself, so in this, if that caused you to have a brain freeze don't worry.
Let's break it down once more, as I said earlier both the String
literal and non-literal declaration are the same, the reason these are the same is behind the scenes the String
literal is actually calling the String()
constructor so for us it's a shortcut so we don't have to do it which removes pretty much all the fun of learning how things work but for seasoned pros makes it far easier to write and maintain their code.
If that at all is confusing don't worry as over time the init
method will become very useful especially during the Build a Weather App with Swift course.
Hope that helps.

Soojin Ro
13,331 PointsIt may be the case only for this specific case. When you start making applications, you will have many more variables and much more complicated classes. Surely you would almost never have a class with just one variable. Maybe you will have 5 variables, which only three of them are already known and two are not, in which case you need the init method.
FYI, init method is used when initialising an instance of a class, so all classes have at least one initialiser (Even your first line of code has an initialiser - it is just automatically generated)

Mark Pittam
1,084 PointsThanks for the reply. Using init() certainly makes more sense when variables are unknown, and, as you point out, I guess that is going to be much more likely to happen in the "real world" of app creation!
Thanks again!
Mark Pittam
1,084 PointsMark Pittam
1,084 PointsThanks very much for the reply. I think it's starting to make sense...