O'Reilly logo

Swift Programming: The Big Nerd Ranch Guide by John Gallagher, Matthew Mathias

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Escaping and Non-escaping Closures

Swift also allows you to use closures that cannot possibly create strong reference cycles. This kind of closure, called a non-escaping closure, does not require you to explicitly refer to self. Closures declared as function parameters are non-escaping by default. Closures in other contexts, such as properties like netWorthChangedHandler, are escaping.

What does escaping mean? Escaping means that a closure passed to a function may be called after the function returns. That is, it escapes the scope of the function it is passed to. If a closure does not escape, the compiler knows that it cannot be called after the function returns, so no strong reference cycle is possible.

You can see this in action. ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required