0

I've started to learn swift a couple days ago and i come from a java/c++ background. However this piece of a code has a mistake that i cant seem to be able to fix.

class Student
{
    var number:Int
    var name:String
    var course:String

    init(_ name:String,_ number:Int, _ course:String)
    {
        self.number = number
        self.name = name
        self.course = course
    }

    func write()
    {
        print("Name:",self.name)
        print("Number:",self.number)
        print("Course:",self.course)
    }
}

class ExchangeStudent:Student
{
    var country:String

   init(_ name:String,_ number:Int, _ course:String, _country:String)
    {
        super.init(name,number,course)
        self.country = country
    }

}

However i get the following error:

property 'self.country' not initialized at super.init call

And i don't really understand how i can solve it

Maytweeer
  • 1
  • 1

1 Answers1

1

The call to super.init needs to come after you have initialised all extra properties added by the subclass.

class ExchangeStudent: Student {
    var country:String

    init(_ name:String,_ number:Int, _ course:String, _ country:String) {
        self.country = country
        super.init(name,number,course)
    }

}
Dávid Pásztor
  • 51,403
  • 9
  • 85
  • 116