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

Jamie Baker
Jamie Baker
5,703 Points

why is my code refusing to accept the 'super.init' call?

class Product { var title: String var price: Double

init(tit: String, pri: Double) {
    self.title = tit
    self.price = pri
}

func discountedPrice(percentage: Double) -> Double {
    return price - (price * percentage / 100)
}

}

enum Size { case Small, Medium, Large init() { self = .Small } }

class Clothing: Product { var size = Size()

let designer: String
init(tit: String, pri: Double, designer: String) {
    self.designer = designer
    super.init(tit: title, pri: price)
}

this is my code. but an error is coming up on the last line ("super.init(tit: title, pri: price)") saying "Use of property 'title' in base object before super.init initialises it".

I have followed every step taken by Amit in the video and can't work it out because I have initialised it in my base class.

Any help would be much appreciated.

In your base class you may need to first run the super.init() method to set the super class' properties and then set your designer attribute:

init(tit: String, pri: Double, designer: String) {
    // Run super class init method to set tit, pri properties
    super.init(tit: title, pri: price)
   // Set base class designer property
    self.designer = designer    
}

2 Answers

Jamie Baker
Jamie Baker
5,703 Points

do you mean in the subclass? you can't declare super.init in the base class. and in the subclass you can't declare super.init before you've set the designer constant.

Yes, I am talking about your base class because the super keyword represents the super class. I've just checked your code and the problem is that in the initializer of your base class you are not passing in the proper variables to the superclasses' constructor:

    init(tit: String, pri: Double, designer: String) {
        self.designer = designer
        // Pass the tit and pri values to the super classes constructor
        super.init(tit: tit, pri: pri)
    }

Hence the error on why the title property was not being set in the super class.

Jamie Baker
Jamie Baker
5,703 Points

thanks a lot mate appreciate it.