[英]How to call the following function in swift without specifying a value for the optional parameter?
In swift it is allowed to have parameters in functions with default values, and it is allowed to have parameters without external names. 快速允许在函数中使用带默认值的参数,并且允许不使用外部名称的参数。 But, what happens when I combine them? 但是,当我将它们结合在一起时会发生什么? For example, in the following code: 例如,在以下代码中:
func foo (a: Int, b: Int = 0, _ c: Int) {
print(a + b + c)
}
Is there any way of call the function foo
without specifying a value for parameter b
? 是否可以在不为参数b
指定值的情况下调用函数foo
方法?
You should have the default parameters at the end of the parameter list. 参数列表的末尾应具有默认参数。
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.