我见过 Swift 中的闭包在内部使用 $0
,有时使用 $1
。 $0
到底是什么?您还可以使用哪些其他 $X
?
以下是它的使用示例:
applyMutliplication(2, {$0 * 3})
array.map({$0 + 1})
最佳答案
这是一个简写参数名称。
来自 Swift 书籍:
“Swift automatically provides shorthand argument names to inline closures, which can be used to refer to the values of the closure’s arguments by the names $0, $1, $2, and so on.”
— Apple Inc. “The Swift Programming Language.”
它有助于减少代码的冗长(有时以可读性为代价),因此您在定义闭包时不必写出长参数列表。
关于ios - Swift 中闭包中的 $0 代表什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41312973/