Swift - Extra Argument in call

前端 未结 6 1964
梦毁少年i
梦毁少年i 2020-11-30 09:40

I am trying to call a function declared in ViewController class from DetailViewController class.

When trying to debug the \'Extra Argument in call\" error pops up.<

6条回答
  •  伪装坚强ぢ
    2020-11-30 10:22

    In latest Swift 2.2, I had a similar error thrown which took me a while to sort out the silly mistake

    class Cat {
        var color: String
        var age: Int
    
        init (color: String, age: Int) {
            self.color = color
            self.age = age
        }
    
        convenience init (color: String) {
            self.init(color: color, age: 1){ //Here is where the error was "Extra argument 'age' in call
            }
        }
    }
    
    
    var RedCat = Cat(color: "red")
    print("RedCat is \(RedCat.color) and \(RedCat.age) year(s) old!")
    

    The fix was rather simple, just removing the additional '{ }' after 'self.init(color: color, age: 1)' did the trick, i.e

     convenience init (color: String) {
            self.init(color: color, age: 1)
      }
    

    which ultimately gives the below output

    "RedCat is red and 1 year(s) old!"
    

提交回复
热议问题