13 Closures

Closures are discrete bundles of functionality that can be used in your application to accomplish specific tasks. Functions, which you learned about in the last chapter, are a special case of closures. You can think of a function as a named closure.

In Chapter 12, you worked primarily with global and nested functions. Closures differ from functions in that they have a more compact and lightweight syntax. They allow you to write a function-like construct without having to give it a name and a full function declaration. This makes closures easy to pass around in function arguments and returns.

Let’s get started. Create a new playground called Closures.playground.

Closure Syntax

Imagine that you are a community ...

Get Swift Programming: The Big Nerd Ranch Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.