我的代码遇到了一个问题,我试图理解为什么对add
的初始调用失败并出现"缺少参数"错误,而当它被传递到另一个函数时成功运行,在不带参数标签的情况下调用它.
我在Why does Swift require parameter names if it also requires argument order?岁时发现了关于这个话题的讨论,但我很难掌握所有细节,也无法确定它与我的查询的相关性.
func add(x: Int, y: Int) -> Int {
x + y
}
func wrapper(function: (Int, Int) -> Int) -> Int {
function(1, 2) // Why does this not produce a "Missing argument" error?
}
// "Missing argument" error
// print(add(1, 2))
// No Error - I think I understand why this works
print(add(x: 1, y: 2)) // Outputs: 3
// How does this work?
print(wrapper(function: add)) // Outputs: 3