Closures are self-contained chunks of code that are first-class citizens in Swift. This means they can be assigned to variables and passed as arguments to functions. Closures may be referred to as blocks or lambdas in other programming languages. Closures can be a bit intimidating at first because of their syntax, but they are very similar to functions and are used extensively within the Swift language. In fact, as we’ll see, a function is a type of closure!
One main way that closures are used throughout Swift is with higher-order functions. Swift provides functions that take closures as arguments to perform common programming tasks. Filtering is one example of this. To filter an array, we provide a closure that instructs the filtering function to remove elements that don’t meet a condition.
Let’s get started by looking at the definition of a closure in the following exercise.
Click Next, when you’re ready to get started.