子类中的附加初始化程序会覆盖超类中的初始化程序

时间:2016-12-13 18:47:58

标签: ios swift

我有一个类XYZObject,它继承自ABCObject的一些初始值设定项和方法:

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
    }
}

但是,每当我打电话给XYZObject(withJson: jsonstuff)时,Swift都会给我错误:Incorrect argument label in call (have 'withJson:', expected 'withName:')

我是swift和iOS开发的新手。我在这里想念的是什么?

感谢您的帮助!

1 个答案:

答案 0 :(得分:3)

在Swift中,如果向类添加新的初始值设定项,则类不会继承其基类的初始值设定项。

如果您希望在init(withJson:)课程中提供XYZObject,则需要添加它:

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