Swift Closure Tutorial with Code Examples

In this Swift tutorial, you will learn how to create a very simple Closure and how to pass it to a function as an argument.

Swift Closure is a block of code starting with { and ending with }, is very similar to a function except that it does not start with a “func” keyword, it does not have a name and can be assigned to a variable, passed around as a variable and be called later. Below is a very simple example of Swift Closure.

// Declare a closure
let simpleClosure = {
    print("From a simpleClosure")
}
// Call closure
simpleClosure()

As you can see in the example above, we are creating a new closure which is just a block of code that is assigned to a variable called simpleClose. And we make a Closure execute its code just like we do it in the case of a function. We can also make a Closure accept one or more parameters and return a value.

A Closure That Returns a Value

A Closure can also return a value. Let’s have a look at another very simple example of a Closure that takes in First Name,  Last Name and returns a Full Name.

let createFullName = { (firstName: String, lastName: String) -> String in
    return "\(firstName) \(lastName)"
}

print(createFullName("Sergey", "Kargopolov"))

A Function That Accepts Closure

You can pass Closure as an argument to a function. Let’s have a look at a short example.

Create Closure

let createFullNameClosure = { (firstName: String, lastName: String) -> String in
    return "\(firstName) \(lastName)"
}

Create a function that accepts Closure

func aFunctionThatTakesAClosure(myClosureArgument:(String, String) -> String) {
    
    // Some function code here   

}

Call a function and pass in a Closure

aFunctionThatTakesAClosure(myClosureArgument: createFullNameClosure)

Run Closure from inside a function

To make a closure run from inside our function we need to simply call it. Like so:

func aFunctionThatTakesAClosure(myClosureArgument:(String, String) -> String) {
    let fullName = myClosureArgument("Sergey","Kargopolov")
    print(fullName)
}

Closure Body as a Parameter

In the example above, we have created a Closure and have assigned it to a variable. Then we used that variable in a function call as a parameter. We can pass a Closure body as a parameter without having it first be assigned to a variable.

Create a function that accepts Closure

func aFunctionThatTakesAClosure(myClosureArgument:(String, String) -> String) {
    let fullName = myClosureArgument("Sergey","Kargopolov")
    print(fullName)
}

Pass Closure Body to a Function

In the code example below a Closure is created and passed as a parameter to a function call at the same time. This Closure is also called a Trailing Closure.

aFunctionThatTakesAClosure() { (firstName: String, lastName: String) -> String in
        return "\(firstName) \(lastName)"
}

If we were to pass the same Closure to the same function but as a variable rather than a block of code, then we would do it this way:

// Create a Closure 
let createFullNameClosure = { (firstName: String, lastName: String) -> String in
    return "\(firstName) \(lastName)"
}

// Pass Closure as a variable to a function call
aFunctionThatTakesAClosure(myClosureArgument: createFullNameClosure)

I hope this short tutorial was helpful to you. If you are interested to learn more about Swift language and how to use it to build iOS apps, then have a look at other Swift tutorials on this site or have a look at the list of online video courses below.

Happy learning!

Leave a Reply

Your email address will not be published. Required fields are marked *