Closures are a very useful part of Swift. An Int is a type that stores an Integer, a String is a type that holds a string. In the same way, a closure is, basically, a type that holds a function. With this capability, you can do many things with closures that you couldn’t do in several older languages. You can assign closures to a variable, you can pass them as arguments to other functions, you can even return one from a function.
Closures in Swift are similar to blocks in Objective-C. In fact, when you call an Objective-C API from Swift that wants a block, you pass in a closure. The major difference between them is coding style, and that closure expressions are much easier to read and write in Swift.
[Read more…]