Swift 3 first parameter names

前端 未结 5 829
慢半拍i
慢半拍i 2020-11-27 20:12

In Swift 2, it appears that the first parameter name is not always required when calling a function. Now in Swift 3, the first parameter name is required when calling the f

5条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-27 21:00

    Exactly. In Swift 3.0, it's mandatory to write parameter names for all the parameters (including the first parameter). Parameter name is the one which is used inside the function implementation body.

    func frobnicate(runcible: String) { 
        print("Frobnicate: \(runcible)") 
    }
    

    By default, the external parameter label is same as the parameter name, if you don't specify any parameter label explicitly. Parameter label is the one which is used to pass the arguments while calling the function. If you need, for better clarity purpose, you can also specify external parameter labels explicitly. Example below-

    func frobnicate(runcibleExternalLabel runcible: String) { 
        print("Frobnicate: \(runcible)") 
    }
    

    If you want to skip the external parameter label while calling the function, just prepend a "_" before the parameter name.

    func frobnicate(_ runcible: String) { 
        print("Frobnicate: \(runcible)") 
    }
    

提交回复
热议问题