Need explaining on the double label function declaration

后端 未结 3 1160
情深已故
情深已故 2021-01-23 22:56

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:          


        
3条回答
  •  孤独总比滥情好
    2021-01-23 23:54

    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).

提交回复
热议问题