Updated 30 April 2021
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 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.
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.
1 2 3 |
{ (parameters) -> return type in statements } |
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
1 2 3 |
var <variable_name> = { (parameters) -> return type in statements } |
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:
1 2 3 4 5 6 7 8 9 |
//initializing the simple closure let example = { print("Hello World") } // calling the closure example() // OUTPUT :- Hello World |
2. Closure that takes the parameters only
1 2 3 4 5 6 7 8 9 10 11 12 |
// initializing the closure var example: (String) -> () = { name in print(name) } // calling the closure example("Hello World") //OUTPUT :- Hello World |
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
1 2 3 4 5 6 7 8 9 10 11 12 |
// declaring the closure let example:(String) -> (String) = { name in let message = name + "Swift" return message } let result = example("Hello") print(result) //OUTPUT :- HelloSwift |
We have defined the parameter type and return type as String.
4.Closure as a parameter to a function
1 2 3 4 5 6 7 8 9 10 11 |
func exampleFunction(exampleClosure:()->()) { print("Function is Called") } exampleFunction(exampleClosure: { print("This is closure") }) //OUTPUT :- Function is Called |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
func exampleFunction(exampleClosure:()->()) { print("Function is Called") exampleClosure() } exampleFunction(exampleClosure: { print("This is closure") }) //OUTPUT :- Function is Called This is 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.
When the function uses the closure as the last parameter.
1 2 3 4 5 6 7 8 9 10 11 12 |
func exampleFunction(msg:String, exampleClosure:()->()) { print(msg) exampleClosure() } exampleFunction(msg:"Hello") { print("World") //closure parameter } // OUTPUT :- Hello World |
Instead of calling closure inside the parameter while calling the function, we can provide the value to closure inside the body.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
func exampleFunction(exampleClosure:() -> Void) { print("function called") exampleClosure() return } exampleFunction { print("closure called") } // OUTPUT:- function called closure called |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
var oncompletion:(() -> Void)? func exampleFunction(exampleClosure: @escaping () -> Void) { print("function called") exampleClosure() // Store completion handler oncompletion = exampleClosure return } func secondExampleFunction() { oncompletion?() // Call closure } } exampleFunction(exampleClosure: { print("closure called") }) //OUTPUT :- function called closure called |
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.
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.
In conclusion, this post may be helpful for developers who are starting their careers in iOS App Development.
For other blogs Please click here
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.