user6510422 user6510422 - 1 month ago 7
Swift Question

Swift 3 first parameter names

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 function. For example:

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


Swift 2.2 allowed the function to be called by simply typing:

Frobnicate("Station")


Swift 3 seems to be requiring that we use the first parameter names of methods such as:

Frobnicate(runcible:"Station")


Is this the case with Swift 3 for all functions and methods or just certain situations?

Answer

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

Comments