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 trialTodd Stacy
5,816 Pointsinitializer help please
i think im just bad at reading directions or something. i understand everything in the videos, but i have no idea why im getting an error on line 23. the comments are other tries that failed also. it says im missing an argument in the call.
class Vehicle {
var numberOfDoors: Int
var numberOfWheels: Int
init(withDoors doors: Int, andWheels wheels: Int) {
self.numberOfDoors = doors
self.numberOfWheels = wheels
}
}
// Enter your code below
class Car: Vehicle {
let numberOfSeats: Int
override init(withDoors doors: Int, andWheels wheels: Int) {
super.init(withDoors: doors, andWheels: wheels)
self.numberOfSeats = 4
}
}
//let someCar = Car(withDoors: Int, andWheels: Int)
//let someCar = Car(doors: Int, wheels: Int)
let someCar = Car(withDoors doors: Int, andWheels wheels: Int)
1 Answer
Steven Deutsch
21,046 PointsHey Todd Stacy,
Let me see if I can clear a few things up for you. First, this is a scenario where you don't need an initializer for your Car subclass. The purpose of an initializer is to make sure all of an objects properties have values when it is created. The challenge asks you to assign a default value for the numberOfSeats property. This means, to create an instance of Car, you just have to call the initializer of the parent class, because all the properties of the Car class that are not being inherited already have values.
class Vehicle {
var numberOfDoors: Int
var numberOfWheels: Int
init(withDoors doors: Int, andWheels wheels: Int) {
self.numberOfDoors = doors
self.numberOfWheels = wheels
}
}
// Enter your code below
class Car: Vehicle {
var numberOfSeats = 4
}
let someCar = Car(withDoors: 4, andWheels: 4)
Second, remember how I just said that the job of an initializer is to make sure the objects properties are set with values? This means when you call the initializer, you have to pass it the values of the expected type to populate the properties. You were trying to pass in types and not actual values. The types are already defined in the class itself.
Also, the reason why we use withDoors and andWheels when calling the initializer is because these are external parameter names that have been specified. Remember, functions (initializers) have local parameter names and external parameter names. If the external name is not specified, the local name will be used as the external name. However, if an external name is specified, it has to be used when making the function call.
Good Luck