Mobile Development

Concepts and Closures

In this chapter, we will talk about the concept of asynchronous programming, an overview of REST and then move onto a new Swift programming idiom, closures.

Asynchronous Behavior

Communicating to a network is essential to perform operations such as logging in, account registration and content sharing.

Let's discuss the concept of asynchornous behavior with an example.

Example - Logging into a server

Let's say you are making an app that asks a user to log in to the app with an email address and a password before continuing.

What if you wanted to show an activity spinner so your app still looks responsive? And what if you wanted the user to cancel the login operation if it took too long?

That's asynchronous behavior. We can wait for a web server to return our authentication check, while still allowing the app to appear responsive. We can start jumping in later chapters that can illustrate this behavior.


REST is an architecture style used for creating web services which allow anything connected to a network to communicate with one another via HTTP. [2]

REST communicates to the server using the standard HTTP verbs, which are:

  • GET - Retrieves data
  • POST - Creates new data
  • PUT - Updates existing data
  • DELETE - Deletes data on the server

Status codes are also defined by HTTP. The most common are

  • 200 - OK
  • 401 - Unauthorized
  • 404 - Not found
  • 500 - Internal server error

REST endpoints are typically nouns which describe the action taking place. For example, this is a theoretical server which allows for customer login and customer retrieval based on name:

# Endpoint Verb Parameters Posted data
1 GET ?name=donald None
2 POST None {'user':'jeff', 'password':'1234'}
3 PUT ?name=jeff {'password':'9876'}
4 DELETE ?name=jeff None

In order, the previous commands

  1. Retrieve a customer object named donald
  2. Create a new customer object named jeff
  3. Update the customer object named jeff
  4. Delete the customer object named jeff

Most APIs that you will write against will contain their own detailed API which will describe what the server expects and what the server will return.

In order to implement REST APIs, we will be using closures.


Programming with the expectation of asynchronous execution requires certain blocks of functionality will be executed at different times. Swift includes closures for just this purpose.

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


Let's check out an example, where we are expecting a block of code to execute after some asynchronous behavior completes.

func helloIsAnyoneThere(completion:(response: String) -> Void) {
    sleep(10) // wait for 10 seconds
    completion(response: "Yes, I am here")

NSLog("Let's see if anyone is there...")
helloIsAnyoneThere { (response) -> Void in
    NSLog("We received a response!  And that response is \(response)")

The above code will be calling a function with a closure as a parameter. The function itself is going to sleep for 10 seconds, this is to simulate a long running process or event. Once those 10 seconds are complete, the closure will be called.

16:35:50 MyPlayground: Let's see if anyone is there...
16:36:00 MyPlayground: We received a response!  And that response is Yes, I am here

Note that the timestamps are 10 seconds apart. If you were to cut and paste this code example into Playgrounds, you'll notice that delay upon execution.

The syntax to define a closure is shown on our first code block and can be written in this way:

{ (parameters) -> return type in

Closures are used in other ways as well. Inline functions, sorting blocks and others are discussed in Apple's Closure documentation [see 1 below].


[1] Closures -

[2] REST -