Swift subclassing - how to override Init()

前端 未结 4 596
面向向阳花
面向向阳花 2021-02-01 13:40

I have the following class, with an init method:

class user {
  var name:String
  var address:String

  init(nm: String, ad: String) {
    name = nm
    address          


        
4条回答
  •  滥情空心
    2021-02-01 13:51

    In swift 2.0 and Later it works like this (all cases)

    init(newString:String) {
        super.init(string:newString)
        // Designed initialiser 
    }
    override init(someString: String) {
        super.init(mainString: someString)
        // Override initialiser when subclass some class 
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
        // Some boilerplate code to handle error (needed when override)
    }
    convenience init(newString:String, withParameters:Dictionary) {
        self.init(someString:newString)
        //Convenience initialiser 
    }
    

提交回复
热议问题