Closure Expression Syntax

This works, but you can clean up your code. Closure syntax follows this general form:

{(parameters) -> return type in
     // Code
}

You write a closure expression inside of the braces ({}). The closure’s parameters are listed inside of the parentheses immediately after the opening brace. Its return type comes after the parameters and uses the regular syntax. The keyword in is used to separate the closure’s parameters and return type from the statements inside of its body.

Refactor your code to use a closure expression: create a closure inline instead of defining a separate function outside of the sort(_:) function.

Listing 13.3  Refactoring your sorting code

import Cocoa var volunteerCounts = [1,3,40,32,2,53,77,13] ...

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.