layout | title | description | redirect_from | ||||||
---|---|---|---|---|---|---|---|---|---|
default |
Swift 4 mini guide: closures |
A mini reference guide for closures in Swift 4. |
|
{:.alert.alert-warning} This page has moved to swiftly.dev/closures-mini! The archive below will not be updated.
The mini version of the [closures guide]({{ "/swift-closures" | relative_url }}).
let sorted1 = [4, 30, 7].sorted(by: { (x: Int, y: Int) -> Bool in
return x < y
})
// Equivalent shorter version:
let sorted2 = [4, 30, 7].sorted { $0 < $1 }
func multiply(x: Int, y: Int, completion: @escaping (Int, Error?) -> Void) {
completion(x * y, nil)
}
multiply(x: 5, y: 6) { print($1 ?? $0) } // Output: 30
Note: Because this closure is escaping, it could be called even after the function returns.