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 Inheritance Convenience Initializers

boris said
boris said
3,607 Points

Error in Convenience initializer: Cannot invoke clothing.init with an argument list of type string,price: Int designer:

string

Here is my code

import UIKit
class Product {

    let title: String
    var price: Double = 0.0

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

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

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

class Clothing: Product {

    var size = Size()
    let designer: String

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

    convenience init (title: String) {
        self.init(title: title, price: 99, designer: "Someone")
    }
    override func discountedPrice(percentage: Double) -> Double  {
        return super.discountedPrice(10.0)
    }
    }

var tshirt = Clothing(designer: "Prada", title: "A shirt", Price: 1000.00)

tshirt.title
tshirt.size = .Large
tshirt.size

let quadcopter = Product(title: "Quadcopter", price: 499.99)

1 Answer

Richard Lu
Richard Lu
20,185 Points

Hey Boris,

The error that you're receiving is because of the initializing signature. Your init method must take the arguments in order. For example:

init(designer: String, title: String, Price: Double) {  // this initializer first takes a designer, a title, and a Price (case sensitive)
   self.designer = designer
   super.init(title: title, price: Price)  // change the lower case 'p' to 'P' in price. MUST BE CONSISTENT 
}

when you used this initializer, you called

convenience init (title: String) {
    self.init(title: title, price: 99, designer: "Someone")   // takes a title first, then a price, and lastly a designer. 
}

Your class does not have any initializers with that initializing signature. Here's the final result:

class Product {

    let title: String
    var price: Double = 0.0

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

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

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

class Clothing: Product {

    var size = Size()
    let designer: String

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

    convenience init (title: String) {
        self.init(designer: "Someone", title: title, Price: 99)
    }
    override func discountedPrice(percentage: Double) -> Double  {
        return super.discountedPrice(10.0)
    }
}

var tshirt = Clothing(designer: "Prada", title: "A shirt", Price: 1000.00)

tshirt.title   // might want to remove
tshirt.size = .Large
tshirt.size   // might want to remove

let quadcopter = Product(title: "Quadcopter", price: 499.99)

Happy coding! :)