Matt
Matt

Reputation: 119

Additional initializer in a sub class overrides an initializer in the super class

I have a class XYZObject which inherits from ABCObject with some initializers and methods:

class XYZObject: ABCObject {
    var name: String = "" 

    init(withName name: String){
        self.name = name
    }
}

class ABCObject{
    internal var jsonstore: JSON

    init(withJson newJson: JSON){
        jsonstore = newJson
    }
}

However, whenever I now call XYZObject(withJson: jsonstuff) Swift gives me the error: Incorrect argument label in call (have 'withJson:', expected 'withName:')

I am fairly new to swift and iOS development. What did I miss here?

Thanks for helping out!

Upvotes: 0

Views: 40

Answers (1)

rmaddy
rmaddy

Reputation: 318804

In Swift, a class does not inherit the initializers of its base class if you add a new initializer to the class.

If you wish to make init(withJson:) available in your XYZObject class, you need to add it:

override init(withJson newJson: JSON) {
    super.init(withJson:newJson)
}

Upvotes: 3

Related Questions