When reading Swift's documentation, Apple commonly uses functionName(_:name:)
or something similar. What exactly is this pattern that sometimes is _:_:
, sometimes just _:
, and _:name:
. I think it has to do with parameter shorthand, but I'm not sure, and can't find the explanation in Swift's programming guide. Thank you!
Example:
insert(_:atIndex:)
In Swift, the underscore operator (_) represents an unnamed parameter/label. In for loops, using the underscore looping parameter is practical if you do not need the looping parameter in the loop.
In the documentation it is being used as a wildcard to indicate a function that takes an unnamed parameter.
When navigating through Swift APIs you'll often run into the @available attribute. We've just covered the #attribute which is similar but just a bit different. The shortest answer to describe its difference: @available is used to mark the availability for a class or method.
The argument label is used when calling the function; each argument is written in the function call with its argument label before it. The parameter name is used in the implementation of the function. By default, parameters use their parameter name as their argument label.
The underscore indicates that there is no external parameter name for the function. Apple's Swift Documentation talks about this concept in terms of when you're writing your own functions.
Take the case where you write this function (from the documentation):
func sayHello(to person: String, and anotherPerson: String) -> String { ... }
If you were to use the function you would write the following:
sayHello(to: "Bill", and: "Ted")
The signature for this would be sayHello(to:and:)
.
However, what if we wanted to use the function as sayHello("Bill", "Ted")
? How would we indicate that we didn't want an external parameter name? Well that's where the underscore comes in. We could rewrite the function as:
func sayHello(person: String, _ anotherPerson: String) -> String { ... }
Note the first parameter doesn't need the _, but subsequent ones will. The first is inferred to have no parameter name. This makes the method signature for this call sayHello(_:_:)
because you as the caller don't have a named parameter.
Update Swift 3.0:
Swift 3.0 treats all parameters equally. The first parameter now requires an underscore to indicate the absense of an external parameter name. In the above example of having sayHello("Bill", "Ted")
at the call site, your corresponding function or method declaration would have to be
func sayHello(_ person: String, _ anotherPerson: String) -> String { ... }
Note the addition of the underscore before the internal parameter name 'person'.
The underscore indicates an ignored value. You can read more about it here: What's the _ underscore representative of in Swift References?
In the documentation it is being used as a wildcard to indicate a function that takes an unnamed parameter.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With