Swift subclassing - how to override Init()

前端 未结 4 592
面向向阳花
面向向阳花 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条回答
  •  闹比i
    闹比i (楼主)
    2021-02-01 13:57

    Have you tried setting a value to numberPriorVisits and changing the types for the calls to super

    class user {
        var name:String
        var address:String
    
        init(nm: String, ad: String) {
            name = nm
            address = ad
        }
    }
    
    
    class registeredUser : user {
        var numberPriorVisits: Int;
    
        init(nm: String, ad: String) {
            self.numberPriorVisits = 0;
            super.init(nm: nm, ad: ad)
        }
    }
    

提交回复
热议问题