Code 101: Closures

Written by: Reinder de Vries, August 23 2015, in Code 101, Guide, Programming

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:

Join 11.000+ app developers and marketers
  • Every Monday: get curated app marketing news, resources, tools and goodies in your inbox
  • Grow your app's userbase with the latest App Store Optimization and app marketing strategies
  • FREE: Get the free step-by-step Get Started With Your App Idea guide to set yourself up for App Store success!
Yes, Sign Me Up!

Popular Posts

Written By: Reinder de Vries

Reinder de Vries is an indie app maker who teaches aspiring app developers and marketers how to build their own apps at He has developed 50+ apps and his code is used by millions of users all over the globe. When he’s not coding, he enjoys strong espresso and traveling.

Get instant access to The Step-by-Step Guide: Get Started With Your App Idea

(PLUS: see the tool we use to save a ton on back-end coding)


Comments & Thoughts

On The Blog