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 Object-Oriented Swift Value vs Reference Types Final Challenge

Lei zheng
Lei zheng
7,990 Points

Below you will find two classes that don't compile. Click on the preview button to see the errors. Look at the errors an

I can not get it right. especially how to set up default value in subclass. I need help here.

Vehicle.swift
class Vehicle {
    let wheels: Int
    let doors: Int

    // Designated initializer
    init(wheels:Int, doors:Int){

    }
}

class Car: Vehicle {
    // A car must default to 4 wheels and 4 doors
    init(){
    self.wheels = 4
    self.doors = 4 
    super.init
      // call super.init
    }
}

As base class, you have to initialize its own stored properties: wheels and doors by using self.wheels = wheels and self.doors = doors And then in subclass, you call designated initializer of superclass, give the value of 4 to wheels and doors with this sentence: super.init(wheels: 4, doors: 4).

Please check out the codes below.

class Vehicle { let wheels: Int let doors: Int

// Designated initializer
init(wheels:Int, doors:Int){
    self.wheels = wheels
    self.doors = doors
}

}

class Car: Vehicle { // A car must default to 4 wheels and 4 doors init(){ // call super.init super.init(wheels: 4, doors: 4) } }

Lei zheng
Lei zheng
7,990 Points

thank you. so the super.init() should be under init(). that's the key. thank you.