O'Reilly logo

Swift 3 Functional Programming by Dr. Fatih Nayebi

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

Closures

Closures are functions without the func keyword. Closures are self-contained blocks of code that provide a specific functionality and can be stored, passed around, and used in the code like functions. Closures capture the constant and variables of the context in which they are defined. Although closures are equivalent of blocks in Objective-C, they have a simpler syntax in Swift compared to the C and Objective-C block syntax. Nested functions, which we have covered in a previous section, are special cases of closures. Closures are reference types that can be stored as variables, constants, and type aliases. They can be passed to and returned from functions.

Closure syntax

A general closure syntax is as follows:

{ (parameters) -> ReturnType ...

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