Closures In Swift

Introduction

In this article, we will learn how we can  apply the concepts of Closures in real projects. We will discuss in some detail its theory part.

What are Closures?

  • In Swift, Closures are basically the function without the names. It is designed for the variables, which means you assign it into the variable and pass as a parameter in the functions.
  • By using Closures, we divide our code into reusable chunks.
  • Closures have very little and short syntax, which makes it easier for the developers to use than other functions.
  • The Closures mean that we “close over” the variables and constant within a closure scope.
  • By declaring the Closure, first assign it into the variable, give the parameters and then return type.

    code

  • Now, we make a Closure, which takes two parameters as an integer, sums it up and shows the result, given below:

    code

  • In this Closure, first, we declare a simple variable with the name plus numbers and after equalizing, we pass the two parameters , return with the integer type and then after scope, we declare the new constant variable with the name result, call a Closure, pass two numbers and you will see that the sum result appears.

Ways of Short Syntax?

  1. If we compare with the Function; Closures are lightweight. There are many ways to make Closure syntax small. First, you can get rid of the return type within a Closure, which means it can’t use the return keyword. After removing the return keyword, you will see that the code is working fine.

    code

  2. In a second way, we remove the return parameter type, which is int and after that our code works fine because it is what Swift is known for. When the user passes the integer value, the result should be an integer so Swift gives us a permission to omit the int keyword as a return type,

    code

  3. In a third way, we declare a Closure in the global scope. Give only the parameters and remove the data type Int, which is declared in round braces.

    code

Addition Using Closure

code

First, we declare a function name called OperationOnNumbers, which takes the two parameters Int, the third parameter is operation and it appears as a function type and OperationOnNumbers returns Int itself. Afterwards, we declare a Closure, which is independent and it means you can create any type of Closure i.e. plus, minus, multiply etc.

Closure with no return Value

  • In the previous topic, you saw that all the Closure takes one or more parameters and also has a return value but just like a function , you also declare Closure, which can’t take any parameter and returns nothing.

    code

  • We declare a constant Closure with empty round brackets which means, it can’t take any parameter, return type is void. We write a print statement and out of scope, we call Closure.

Array Sorting Using Closure

  • With the help of Closure, we make sorting in an array very easy.

    code

  • First, we declare a constant array, which is unsorted and then we make a Closure, which takes two parameters and then compares both the parameters and places them in another array.


Similar Articles