- Mastering macOS Programming
- Stuart Grimshaw
- 95字
- 2021-07-02 22:54:33
Alternative closure syntax
There is a way to automatically name the arguments in a closure, leading to a slightly more concise syntax. The preceding closure could have been written as follows:
let myIntOp: (Int, Int) -> Int = { return $0 * $1 }
So $0, $1... are symbols for the arguments passed to a closure.
In the case of such one-liners, we can even omit the return keyword:
let myIntOp: (Int, Int) -> Int = { $0 * $1 }
This may look odd at first, but you get used to it pretty quickly.