iOS Ref

Swift 4 mini guide: closures

This page has moved to swiftly.dev/closures-mini! The archive below will not be updated.

The mini version of the closures guide.

Closure inline

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 }

Closure as a function parameter

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.