Important facts of closures in Swift
You can write a closure without a name by surrounding code with braces ({}). Use in in a closure to separate the arguments and return type from its body.
let numbers = [1, 2, 3, 4, 5, 6]
numbers.map({ (number: Int) -> Int in
let result = number * 4
return result
})
You can omit the type of its parameters, its return type, or both, when a closure's type is already known.
Single-statement closures implicitly return the value of their only statement.
let numbers = [1, 2, 3, 4, 5, 6]
numbers.map({ number in number * 3 })
You can refer to parameters by number instead of name. $0 is the first input parameter for the closure. If the closure had multiple parameters, the second would be $1, the third $2, and so on.
numbers.map({ $0 * 3 })
When a closure is the only argument to a function, you can omit the parentheses entirely.
numbers.map { $0 * 3 }
numbers.sorted { $0 > $1 }
A closure passed as the last argument to a function can appear immediately after the parentheses.
numbers.reduce(0) { $0 + $1 }