Swiftly

Swift 5.7 references for busy coders

Closures (mini version)

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.