From the Swift docs:
Function parameters can have both a local name (for use within the function’s body) and an external name (for use when calling the function), as described in External Parameter Names. The same is true for method parameters, because methods are just functions that are associated with a type. However, the default behavior of local names and external names is different for functions and methods.
...
...
Specifically, Swift [makes] the first parameter name in a method a local parameter name by default, and [makes] the second and subsequent parameter names both local and external parameter names by default. This convention matches the typical naming and calling convention you will be familiar with from writing Objective-C methods, and makes for expressive method calls without the need to qualify 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—amount and numberOfTimes. By default, Swift treats amount as a local name only, but treats numberOfTimes as both a local and an external name. You call the method as follows:
let counter = Counter() counter.incrementBy(5, numberOfTimes: 3) // counter value is now 15
You don’t need to define an external parameter name for the first argument value, because its purpose is clear from the function name incrementBy. The second argument, however, is qualified by an external parameter name to make its purpose clear when the method is called.
This default behavior effectively treats the method as if you had written a hash symbol (#) before the numberOfTimes parameter:
func incrementBy(amount: Int, #numberOfTimes: Int) { count += amount * numberOfTimes }
The default behavior described above means that method definitions in Swift are written with the same grammatical style as Objective-C, and are called in a natural, expressive way.
So, to nullify the external name of the second parameter of a method you can explicitly write '_' for the external name:
class Counter { var count: Int = 0 func incrementBy(amount: Int, _ numberOfTimes: Int) { count += amount * numberOfTimes } }
Now the syntax for the method name becomes:
incrementBy(__:__:)
The syntax tells you how to call the method. In this case, the syntax tells you that there are two un-named method arguments, so you call the method like this:
incrementBy(3, 2)
If the method name is incrementBy(_:numberOfTimes:), the syntax tells you that the first argument is un-named while the second argument's name is numberOfTimes, so you call the method like this:
incrementBy(3, numberOfTimes:2)