I was reading the apple doc when I fell upon this piece of syntax :
struct Point {
var x = 0.0, y = 0.0
mutating func moveBy(x deltaX: Double, y deltaY:
The first label is Argument Labels and the second label is Parameter Names.
From Apple Document:
Each function parameter has both an argument label and a parameter name. 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.
Usage:
func foo(with hoge: Int) {
return hoge * 2
}
let A = foo(with: 2) // A = 4;
Look closely to the Apple documentation.It is connected with Parameter Names.
You can override the default behavior for argument labels with one of the following forms:
argument label parameter name: parameter type
_ parameter name: parameter type
Short answer: first argument label is for external caller, second one for local in-method use.
func moveBy(x deltaX: Double, y deltaY: Double)
when calling looks following: moveBy(x: 1, y: 1)
, but inside the method deltaX
and deltaY
labels are used.
This naming style is not necessary, you can declare the method func moveBy(x: Double, y: Double)
so x
and y
will be used inside the method.
To support legacy style, so from caller scope your method looks like moveBy(1, 1)
, you should place _
as first argument label: func moveBy(_ deltaX: Double, _ deltaY: Double)
. Such declarations are used in CocoaTouch to support legacy obj-c interface (ex. func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool
).