O'Reilly logo

Swift Quick Syntax Reference by Matthew Campbell

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

Chapter    29

Closures

Closures are blocks of code that you can pass to functions as parameters or store as variables or constants to be used later. Closures capture the state of the other variables around them. These constructs are often used when you want to perform operations that don’t need to happen immediately but depend on local state. Closures are also used frequently with functions to give callers a way of adding behavior to a function that would normally be out of scope.

One example of where closures are used is as an argument to the Swift standard library sorted function. This function takes an array and a closure as parameters. The array contains the items that need to be sorted, and the closure includes the instructions that will ...

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