Swift 3 first parameter names

前端 未结 5 808
慢半拍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 20:47

    The reason you want labels for parameters is so other code can supply parameters in any order.

    Without labels, when you call the function the parameters are anonymous and you cannot be certain if you have supplied them in the wrong order.

    Place labels in front of the parameters and code tools can do a much better job at catching errors us humans put in.

    The underscore is just a way to cope with the transition from legacy code; method names including an Implicit first parameter. You should change any underscores that migration adds to your code to an explicit parameter label. You know it makes sense.

    0 讨论(0)
  • 2020-11-27 20:52

    Yes, this is right. Swift is fixing a language inconsistency this way (this was always required for initializers).

    If you don't want to use the external parameter name, just remove it explicitly:

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

    You can read the full rationale in Swift Evolution 0046

    0 讨论(0)
  • 2020-11-27 20:56

    Yes Swift 3 requires you to send First Parameter Label.

    Please refer Swift 3 changes

    0 讨论(0)
  • 2020-11-27 21:00

    You can read The Swift Programming Language (Swift 3) in i-Book. Also you can check this out in WWDC 2016: What's New in Swift

    In Swift 3, by default, functions use their parameter names as labels for their arguments. Write a custom argument label before the parameter name, or write _ to use no argument label.

    fun greet(_ person: String, on day: String) -> String {
        return "Hello \(person), today is \(day)."
    }
    greet("John", on: "Wednesday")
    

    or

    // This also works with Swift 2
    fun addThreeNumber(_ first: Int, _ second: Int, _ third: Int) {
        print(first+second+third)
    }
    addThreeNumber(1, 2, 3)
    
    0 讨论(0)
  • 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)") 
    }
    
    0 讨论(0)
提交回复
热议问题