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

Sam Belcher
Sam Belcher
3,719 Points

Classes

Hi, I don't have a firm understanding of inits, why the self. is needed, when to override an init, how you can assign a variable a type of the same name in a super init, what the point of a super init is, when to use a super init, and when to write additional code under a super init. sorry for the length of this lol.

1 Answer

For your first question, you need to use self so that the compiler know which properties you are referring to (this is in the case of same property name and same argument name).

class SamleClass {
     let firstProperty: String

     init(firstProperty: String) {
        self.firstProperty = firstProperty
     }
}

Second. you use override if you have same init implementation to the superclass. If you want to change the behavior of the init on the superclass, that's the time you use override init.

Third, remember that every time you call super it is referring to the superclass. So if you have method or init method in the super class and you want to use that in the child class you use super, or in this case super.init.