以下代码未编译,但在我看来它应该:
public typealias ACallback = (first: [Int], second: String, third: String, fourth: CustomType) -> [Int];
public func doSomething(callback: ACallback = { $0 } {
...
}
我在函数声明的行上得到一个错误:
'(first: [Int], second: String, third: String, fourth: CustomType)' is not convertible to '[Int]'
当我这样声明函数时,它起作用了:
public fund doSomething(callback: ACallback = { first, _, _, _ in first } {
如果我用相同的内联定义替换 ACallback
也没有区别。
最佳答案
callback: ACallback = { $0.0 }
我想应该没问题。如错误所述,$0
表示此处该闭包的整个参数列表的元组。
关于swift - 为什么我不能在此默认参数值中使用速记参数名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32216909/