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

What is the Purpose of Init()

I don't understand the purpose of the initializer in Swift. Everything seems to work the same without it.

2 Answers

There is always an init method, whether you explicitly write one yourself, or whether you're using the init method that was defined behind the scenes automatically. In a struct, they create an init method for you automatically, but you can create your own. In a class, you have to define an init method or it won't compile. If your class is inheriting from another class, it inherits its init method so you get one automatically, but you can also override it.

The init method is what gets called when you create a new instance of an object, so what happens inside that method is what will happen when it's first created. This is where you would define how the properties of the object get set. Maybe there are default values. Maybe the init method takes arguments that get set as values for your property, and it might modify them before setting the value.

The advantage of being able to write the init method yourself is that you can customize the method to what you want to have happen right off the bat, rather than having to make changes to your object later in other lines of code.

Hello Ethan:

As mentioned by Brendan, an init in a struct is automatically created, but I will give you an example of a class that will required an init to create an instance of this class.

class Car {
    let doors: Int  // this will be self.doors.
    let wheels: Int
    let year: Int
    let name: String

    init(doors: Int, wheels: Int, year: Int, name: String){
        self .doors = doors // this doors is from the parameter.
        self.wheels = wheels
        self.year = year
        self.name = name
    }
}

// When using the word self, you are telling the system to use the store propertie, and not the parameter.

// Making an instance of class Car()

let audi = Car(doors: 4, wheels: 4, year: 2016, name: "Audi S4")

audi.name // prints Audi S4
audi.year // prings 2016

As you notice I created an init method, without it, I would not be able to initialize the store properties inside that class, therefore no proper instance would have been created. Some classes don't require an init method, but that's because they may have their store properties already initialized or they are classes with only methods in them.

Hope this helps you clear a bit.

Good luck.