Closures in Swift

Closures are a big part of Swift and until yesterday I didn’t know anything about them. A closures is a self contained block of functionality. In fact, functions actually are closures, however, not all closures are functions. One of the benefits of using a closure is that it can store information about variables and constants from where the closure was defined. Another benefit to using a closure is they are really flexible.

There are at least two types of closures, normal closures and names closures. Although very similar they have a slightly different syntax.

Normal Closure

{
( a: Int ) -> in
return a + 1
}

 

Named Closure

func increment ( a: Int ) -> Int {
return a + 1
}

I am keep this post very short and going to add more to it when I have a better understanding of how Closures work in Swift. If you are interested in learning more I recommend reading through this post from Level Up Tutts or just checking out the developer library.