swift documentation on closures状态:
Swift’s closure expressions have a clean, clear style, with optimizations that encourage brief, clutter-free syntax in common scenarios. These optimizations include:
- Inferring parameter and return value types from context
- Implicit returns from single-expression closures
- Shorthand argument names
- Trailing closure syntax
Swift 闭包的“尾随闭包语法”究竟是什么?
最佳答案
尾随闭包写在函数调用的括号之后,即使它仍然是函数的参数。当您使用尾随闭包语法时,您不会将闭包的参数标签作为函数调用的一部分编写。
func doSomething(number:Int, onSuccess closure:(Int)->Void) {
closure(number * number * number)
}
doSomething(number: 100) { (numberCube) in
print(numberCube) // prints 1000000
}
函数调用中没有参数标签 onSuccess。即使闭包包含在函数参数列表中,swift 也会将其从参数块中取出,以使代码更具可读性。
关于swift - Swift 中的尾随闭包语法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36762145/