如何在不为可选参数指定值的情况下调用 swift 中的以下函数?

How to call the following function in swift without specifying a value for the optional parameter?

在swift中允许函数中有带默认值的参数,允许有没有外部名称的参数。但是,当我将它们结合起来时会发生什么?例如,在下面的代码中:

func foo (a: Int, b: Int = 0, _ c: Int) {
    print(a + b + c)
}

有没有什么方法可以在不为参数 b 指定值的情况下调用函数 foo

不,你不能。这就是为什么 Apple 在 Swift 书中建议将具有默认值的参数放在参数列表的末尾:

你应该在参数列表的末尾有默认参数。

func foo (a: Int, c: Int, b: Int = 0) {    //put your parameter with default value at the end
    print(a + b + c)
}
//you don't need to specifying a value for parameter b
foo(10, 11)  //21