Swift 5.1: Closures Cheat Sheet

let adder: (Int, Int) -> Int = { (x, y) in x + y }

// Closures with shorthand argument name
let square: (Int) -> Int = { $0 * $0 }

// Passing a closure to a function
let addWithClosure = doMath(operation: adder, a: 2, b: 3)

Leave a Reply

Your email address will not be published. Required fields are marked *