#56: Escaping Closures in Swift

A closure parameter is escaping when the function stores it or executes it later instead of finishing with it before returning.

var handlers: [() -> Void] = []

func register(_ handler: @escaping () -> Void) {
    handlers.append(handler)
}

Swift requires @escaping so the lifetime change is visible in the API.

This matters because escaping closures often capture state, which can lead to retain cycles or unexpected lifetimes if you are not careful.


I'm also on Twitter and GitHub.

Subscribe via RSS.