NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

Question about Class initializers (Day 12)

Forums > 100 Days of SwiftUI

Hey All,

So I'm a bit confused looking at lessions two and three. In lesson two, we seem to hand-write an super initializer to an instance that is not called in the child class. The parent class contains it's own initializer:

class Employee {
    let hours: Int

    init(hours: Int) {
        self.hours = hours
    }

but the child classes do not:

class Developer: Employee {
    func work() {
        print("I'm writing code for \(hours) hours")
    }

When we create an instance of Developer, Paul shows us calling the initializer to the parent class as part of the instance:

let robert = Developer(hours: 8)

However, in lesson three, he discusses that we must call the parent initializers via super.init. Do we only have to call them if the child class also contains an initializer? If the child class does not contain an initializer and we call it, I presume this creates an issue of infilled values in the parent class since memberwise's arent available?

Thanks, Andrew

   

Hi!

Do we only have to call them if the child class also contains an initializer?

Short answer is yes. Just as an explanation this is excerpt from book

If a subclass declares any designated initializers of its own, the entire game changes drastically. Now, no initializers are inherited! The existence of an explicit designated initializer blocks initializer inheritance. The only initializers the subclass now has are the initializers that you explicitly write. Moreover, every designated initializer in the subclass now has an extra requirement: it must call one of the superclass’s designated initializers, by saying super.init(...). If it fails to do this, then super.init() is called implicitly if possible.

As a side note, classes unlike structs do not have memberwise initializers. So we always have to use it with classes. You do not have to use init with classes only when you declare its properties and assign them on spot. In that case they have implicit init() like in example

class Dog {
  var name = "Fido"
}

let dog = Dog()

but if you have it like:

class Dog {
 var name: String

  init(name: String) { // You have to use init here
    self.name = name
  }
}

   

Gotcha. This makes perfect sense. Thank you for your help!

   

If Yuri's answer helped, please mark your post as Solved!

   

here is another way of looking at it - if your subclass has a new property, it needs to be initialized as well - so you have to make a new init for the subclass:

class Animal {
    var numberOfLegs = 4

    init(numberOfLegs: Int) {
        self.numberOfLegs = numberOfLegs
    }
    func speak() {

    }
}

and

class Cat : Animal {

    var isTame: Bool

    init (numberOfLegs: Int, isTame: Bool) {
        self.isTame = isTame
        super.init(numberOfLegs: numberOfLegs)
    }
    override func speak() {

    }
}

so we need to reference super.init in the sub-class - hope this helps

   

Hacking with Swift is sponsored by Judo

SPONSORED Let’s face it, SwiftUI previews are limited, slow, and painful. Judo takes a different approach to building visually—think Interface Builder for SwiftUI. Build your interface in a completely visual canvas, then drag and drop into your Xcode project and wire up button clicks to custom code. Download the Mac App and start your free trial today!

Try now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.