#60: Trailing Closures in Swift

If the last argument of a function is a closure, Swift lets you move it outside the parentheses.

numbers.map { $0 * 2 }

This becomes even nicer with multiple closure parameters:

loadImage(from: url) {
    print("success")
} onFailure: { error in
    print(error)
}

Trailing closures are mostly about readability, but in callback-heavy code that readability gain is significant.


I'm also on Twitter and GitHub.

Subscribe via RSS.