Code 101: Closures

Written by: Reinder de Vries, August 23 2015, in 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:

Join 11.000+ app developers and marketers
  • Get a weekly curated list of app development tools, articles and resources in your inbox
  • 10x your app installs with relevant App Store Optimization and app marketing strategies
  • BONUS: Grab a free copy of the App Toolbox 2017 to supercharge your next app project
Yes, Sign Me Up!

Most Popular Content

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.

Got a killer app idea?

Grab the App Toolbox 2017 to learn how to save time building your app,
and how to 10x your app installs in the App Store. With the toolbox, you'll immediately know how to move forward to build better, more profitable apps.

Get The App Toolbox

Comments & Thoughts

Leave a Reply

Markdown is supported (Learn more). Your email address will not be published. Required fields are marked with *

On The Blog