April 2017
Intermediate to advanced
316 pages
9h 33m
English
A general closure syntax is as follows:
{ (parameters) -> ReturnType in // body of closure }
A closure definition starts with {, then we define the closure type, and finally we use the in keyword to separate the closure definition from its implementation.
After the in keyword, we write the body of the closure and finish our closure by closing }.
Closures can be used to define variables or stored as variables. The following closure defines a variable of a type closure that accepts Int and returns Int:
let closureName: (Int) -> (Int) = { _ in 10 }
Closures can be stored as optional variables. The following closure defines a variable of a type closure that accepts Int and returns Optional Int
var closureName: (Int) -> (Int)? ...
Read now
Unlock full access