1
class Car {

let numWheels: Int

    init(numWheels: Int) {
        self.numWheels = numWheels
    }//end constructor
}//end class


class FastCar: Car {
    let topSpeed: Int
}//end class

why do I get an error saying "class 'FastCar' has no initializers"? I am an extreme beginner to the world of swift, and I feel like I just made a simple mistake but I went over it so many times I decided to come here.

GJZ
  • 2,482
  • 3
  • 21
  • 37
  • 1
    Classes must be fully initialized when they are instantiated. You either need to provide an initializer for FastCar or provide a value for topSpeed. You'll need to initialize numWheels as well. – vacawama Sep 08 '16 at 01:26
  • Possible duplicate of [Class has no initializers Swift](http://stackoverflow.com/questions/27797351/class-has-no-initializers-swift) – Cristik Sep 08 '16 at 02:28

1 Answers1

1

Because your FastCar class has a 'let' variable it needs to have a constructor or be given a value.

class FastCar: Car {
    let topSpeed: Int = 4
}

or

class FastCar: Car {
    let topSpeed: Int
    init(topSpeed: Int) {
        self.topSpeed = topSpeed
        super.init(numWheels: 4) // numWheels provided by init?
    }
}
sbarow
  • 2,759
  • 1
  • 22
  • 37
Ben Trengrove
  • 8,191
  • 3
  • 40
  • 58