From Swift docs :
Function parameters can have both a local name (for use within a function) and an external name (for use when calling a function), as described in "External parameter names". The same is true for method parameters, because methods are simply functions that are associated with a type. However , the default behavior of local names and external names are different for functions and methods.
...
...
In particular, Swift [makes] the name of the first parameter in the local method default parameter name and [makes] the second and subsequent parameter names, both local and external default parameter names. This convention corresponds to the typical naming and calling convention, which you will be familiar with writing Objective-C methods and express calls without having to define your parameter names.
Consider this alternative version of the Counter class ...
class Counter { var count: Int = 0 func incrementBy(amount: Int, numberOfTimes: Int) { count += amount * numberOfTimes } }
This incrementBy (_: numberOfTimes :) method has two parameters - quantity and numberOfTimes. By default, Swift treats the sum as a local name only, but processes numberOfTimes both the local and the external name. You call the method as follows:
let counter = Counter() counter.incrementBy(5, numberOfTimes: 3)
You do not need to determine the name of the external parameter for the first value of the argument, since its purpose is clear from the name of the incrementBy function. The second argument, however, qualifies the parameter name externally to clear its purpose when the method is called.
This default behavior effectively handles the method as if you had a hash (#) before the numberOfTimes parameter:
func incrementBy(amount: Int,
The default behavior described above means that the method definitions in Swift are written in the same grammatical style as Objective-C and are invoked in a natural, expressive way.
So, to invalidate the external name of the second parameter of the method, you can explicitly write '_' for the external name:
class Counter { var count: Int = 0 func incrementBy(amount: Int, _ numberOfTimes: Int) { count += amount * numberOfTimes } }
The syntax for the method name will now look like this:
incrementBy (__: __ :)
The syntax tells you how to call the method. In this case, the syntax tells you that there are two unnamed arguments to the method, so you call the method as follows:
incrementBy(3, 2)
If the method name is incrementBy(_:numberOfTimes:) , the syntax tells you that the first argument is unnamed and the second argument is numberOfTimes, so you call the method as follows:
incrementBy(3, numberOfTimes:2)