What's the _ underscore representative of in Swift References?
Both answers were correct but I want to clarify a little bit more.
_
is used to modify external parameter name behavior for methods.
In Local and External Parameter Names for Methods section of the documentation, it says:
Swift gives the first parameter name in a method a local parameter name by default, and gives the second and subsequent parameter names both local and external parameter names by default.
On the other hand, functions by default don't have external parameter names.
For example, we have this foo()
method defined in class Bar
:
class Bar{
func foo(s1: String, s2: String) -> String {
return s1 + s2;
}
}
When you call foo()
, it is called like bar.foo("Hello", s2: "World")
.
But, you can override this behavior by using _
in front of s2
where it's declared.
func foo(s1: String, _ s2: String) -> String{
return s1 + s2;
}
Then, when you call foo
, it could be simply called like bar.foo("Hello", "World")
without the name of the second parameter.
Back to your case, runAction
is a method because it's associated with type SKNode
, obviously. Thus, putting a _
before parameter action
allows you to call runAction
without an external name.
Update for Swift 2.0
Function and method now work the same way in terms of local and external argument name declaration.
Functions are now called by using external parameter name by default, starting at 2nd parameter. This rule only applies to pure Swift code.
So, by providing an _
in front of a function, the caller won't have to specify external parameter name, just like what you would do for a method.
The underscore is a general token used to indicate a discarded value.
In this specific case, it means that the function will be invoked as runAction(argument)
instead of runAction(action:argument)
In other contexts it has other similar meaning, e.g. in:
for _ in 0..<5 { ... }
It means that we merely want to execute the block 5 times and we don't care about the index within the block.
In this context:
let (result, _) = someFunctionThatReturnsATuple()
It means that we don't care what the second element of the tuple is, only the first.
Since Swift 3 all argument labels are required by default.
You can force an IDE to hide an argument label with _
.
func foo(a: String) {
}
func foo2(_ a: String) {
}
called foo(a: "abc")
and foo2("abc")
Note: This can be used only when
a
is the (external) argument label and (internal) variable name at the same time. It's equivalent -func foo(a a: String)
won't accept the_
.
Why is Apple using it?
You can see Apple is using it across the API. Apple's libraries are still written in Objective-C (if not, they share the same function names anyway, which were designed for Objective-C syntax)
Functions like applicationWillResignActive(_ application: UIApplication)
would have redundant parameter name application
, since there is already the application in it's function name.
Your example
func runAction(_ action: SKAction!)
would be called without it's _
mark like runAction(action:)
.
The parameter name action
would be redundant since there is already one in the function name. That's the purpose and why it's there.