Swift : missing argument label 'xxx' in call

前端 未结 6 775
-上瘾入骨i
-上瘾入骨i 2020-12-04 15:01
func say(name:String, msg:String) {
    println(\"\\(name) say \\(msg)\")
}

say(\"Henry\",\"Hi,Swift\")  <---- error because missing argument label \'msg\' in ca         


        
6条回答
  •  春和景丽
    2020-12-04 15:48

    Swift 3.0 update:

    In swift 3.0, methods with one param name per inputs are required to have that param name as part of the function call. So if you define the function like this

    func say(name:String, msg:String) {
        print("\(name) say \(msg)")
    }
    

    Your function call will have to be like this

    self.say(name: "Henry",msg: "Hi,Swift")
    

    If you want to have English like readable function labels but do not want to change input param name, you can add the label in front of the parameter names, like this

    func say(somethingBy name:String, whoIsActuallySaying msg:String) {
        print("\(name) say \(msg)")
    }
    

    Then calling it like this

    self.say(somethingBy: "Henry",whoIsActuallySaying: "Hi,Swift")
    

提交回复
热议问题