Swift Tutorial

Closures

Anonymous functions, trailing syntax, and shorthand arguments

What is a Closure?

Closures are self-contained blocks of functionality that can be passed around and used in your code. They are similar to "Lambdas" in other languages. In Swift, even functions are actually a special type of closure.
swift
let greet = { (name: String) -> String in
    return "Hello, \(name)!"
}

print(greet("Alpha"))

Trailing Closure Syntax

Wait! If a function's last argument is a closure, you can write it outside the parentheses. This makes code much cleaner and is the foundation of SwiftUI.
swift
// Standard way
names.sorted(by: { $0 > $1 })

// Trailing closure way (Preferred!)
names.sorted { $0 > $1 }

Shorthand Argument Names ($0, $1)

Swift automatically provides shorthand names for the arguments passed to a closure. $0 is the first argument, $1 the second, and so on.
swift
let reversed = names.sorted { $0 > $1 }

Capturing Values

Closures can "capture" and store references to variables and constants from the context in which they are defined. Swift handles all the memory management of these captured values automatically.

Summary

TermDescription
{ }Syntax for a closure
inSeparates parameters/return type from the body
$0, $1Shorthand for closure arguments
TrailingPutting the closure after () if it's the last arg
Escaping@escaping - Closure that outlives the function it was passed to
Best ForCallbacks, sorting, and UI layout (SwiftUI)
Logic"Functions as values"
SafetyStrong type inference for closure parameters
ModernHighly expressive and concise
StandardUse trailing closure syntax whenever possible
IdentityClosures are Reference Types!
AutoclosureWrapping an expression in a closure automatically (Advanced)