Code 101: Closures

Written by: Reinder de Vries, August 23 2015, in App Development

OK, let’s talk about closures for a second. In previous Code 101 episodes we talked about functions and variables. The next logical step is: learning about closures, because closures sorta combine variables and functions. How? Let’s find out!

In this episode of Code 101 I’ll show you how to work with closures, how to use them for fun and profit.

What’s A Closure?

According to the Swift documentation it’s this:

Closures are self-contained blocks of functionality that can be passed around and used in your code.

I rather think of closures as a function that’s stored in a variable. You can pass the variable around in your code, use it as an argument in another method, and call upon it when you need it. Closures in Swift are similar to blocks in Objective-C, and are often called lambda functions in other programming languages.

Now, the cool thing about closures is that they can use the variables around the closure as if they were available inside the closure.

How Can You Use A Closure?

Let’s look at an example in pseudo-code:

var imageView:UIImageView = UIImageView();

Alamofire.downloadFromInternet("") {  
    imageData in

    imageView.image = UIImage(data: imageData);  

OK, what happens here?

  • First, we declare a new image view: a container for a bitmap image.
  • Then, we download the image from the internet with a URL.
  • Finally, when the image has finished downloading, we assign the downloaded image data to the image view and voila: the image displays.

The closure in the code snipped is everything inside the squiggly brackets {}. And the key to understanding it is this: the code in the closure is executed when the image has finished downloading, but it’s written as if it happens immediately. Also, the imageView variable is available for use inside the closure, despite the fact that the code outside the closure and inside the closure happen at 2 distinct and separated moments!

Can I Make My Own Closures?

Sure you can! Imagine you work on the Mars Space Mission, and you’ve just sent a robot to Mars. You want to define what happens after the robot lands on Mars. Perhaps, like this:

func spaceFlight(speed:Int, onCompletion: (() -> Void) )  
    // Do long flight stuff  
    while(areWeThereYet == false)  
        // Journey onwards  


What happens here?

  • First, we declare a new function with func. It’s called spaceFlight and it takes 2 parameters: speed of type Int, and onCompletion of type (() -> Void). That type is a closure definition!
  • Then, we fly and fly and fly and fly until we’re there.
  • Finally, the closure is executed. Do you see it’s just a variable with () attached to it? That’s the power of a closure: it’s a function inside a variable, that behaves like a function and a variable.

Alright! ’Nuf said. Let’s put those closures to work.

Further reading:

Reinder de Vries

Reinder de Vries is a professional iOS developer. He teaches app developers how to build their own apps at Since 2009 he has developed a few dozen apps for iOS, worked for global brands and lead development at several startups. When he’s not coding, he enjoys strong espresso and traveling.

Leave a Reply

Required to post: Your real name and email address, and a pleasant demeanor. Your email address will not be published. Markdown is supported.