Closures in Swift

Updated 30 April 2021

Save

There are different types of Closures in  Swift that we work with.

One question always arises in mind what is the use of Closures.

Here we will discuss why we use closures, how can we use them and how many types of Closures are there in Swift.

Closures in Swift

Closures are another type of function that accepts the parameters and they don’t require any keyword for their declaration.

Like function, closures can have return types and a body to execute the code inside the body.

Use of Closures in Swift

1.Higher-Order Functions

Closure can be passed as the input parameter for the higher-Order functions. It is better to use a closure instead of a function as they are more readable and clear to understand.

2. Completion Handler

Closures help us by notifying us that certain work has been completed.

Syntax of Closure

This is the basic syntax of the closure. Notice the “in” keyword that differentiates the body of the closure with the parameters and return type of the closure.

After that, we can assign a closure to any variable or constants. For example

Now there can be multiple types of closures based on the parameters and return types a closure takes.

1. Closure without parameters and return type:

 

2. Closure that takes the parameters only

The value supplied to closure is taken by the parameter “name”  and is used to work on that value inside the closure body.

3. Closure that returns the value

We have defined the parameter type and return type as String.

4.Closure as a parameter to a function

Here we are passing the closure as a parameter to a function that does not takes any values and does not return any value. The closure block is not executed as we have not called the closure. Now, let’s check by calling the closure.

 

Till now we have learned that multiple forms a closure can take based on the parameter and return types. After that, we are now going to describe the types of closures based on the function.

 

Trailing Closure in Swift

When the function uses the closure as the last parameter.

Instead of calling closure inside the parameter while calling the function, we can provide the value to closure inside the body.

Non-Escaping Closures in Swift

We have called the closure before the return of the function and we are not calling it outside the function. By default all the closure parameters are Non-escaping.

Escaping Closure in Swift

We need to specify the escaping closure with the keyword @escaping.

In addition, to store the closure value in the variable declared “oncompletion” outside the function the closure needs to be escaping from the body of the function.

After that, we can use the value stored in the variable that was declared outside the function in our program.

 

Closure as Completion Handler

We can use closures as the completion handler which tells us that a function has completed its working and now value can be stored inside the completion handler. For instance in the code below when the web view loads the URL  then we call the completion handler callbackResult to hold the value. However, we need to call the completion handler where we need its value.

closure as completion handler

 

Conclusion

In conclusion, this post may be helpful for developers who are starting their careers in iOS App Development.

For other blogs Please click here

author
. . .

Leave a Comment

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


Be the first to comment.

Start a Project


    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home