Sitemap
TanyeliSargut

TanyeliSargut

Closures

4 min readJul 19, 2022

Basics of Closures

Understanding closures can be a little tricky for beginners but if you work on it, read it from different sources and understand it correctly, it’s a great tool for clean and high-performance code.

Closures are self-contained code blocks of functionality. Code blocks are given some tasks to do. As you can understand it’s similar to functions. What is the difference? Well, you don’t have to give a name to closure, it can be stored as a variable.

The Syntax

It can be complicated when you see the closure syntax for the first time, to avoid that situation we will compare function and closure syntax. This will help you to understand better.

Press enter or click to view image in full size
Press enter or click to view image in full size

As you can see above, functions and closures are very similar. There are four types of closures.

  1. A closure with no parameters and no return value.
Press enter or click to view image in full size

2. A closure with parameters and no return value.

Press enter or click to view image in full size

3. A closure with no parameters and a return value.

Press enter or click to view image in full size

4. A closure with parameters and a return value(like the first example).

Press enter or click to view image in full size

Using Closures As Arguments

Many commonly used collection functions and UIKit objects take closures as arguments. Swift has a function named sorted (by: ) which helps you compare two arguments in your Array.

Get TanyeliSargut’s stories in your inbox

Join Medium for free to get updates from this writer.

Now, you can learn how to use a closure as an argument with the example below.

Press enter or click to view image in full size

Bonus: There is a shorter way to write closures. Swift is a very smart programing language so it knows that the sorted function is a comparison function that returns a Bool value. So we don’t have to write the return part. Also, you can use $ as a placeholder name. In this case, $0 is our first parameter and $1 is our second parameter to compare. Our previous example is now as short as a line of code.

Press enter or click to view image in full size

Swift has some other advanced functions that you can use with closures. I will show you a couple of them today.

Map

You can use the Map function with Arrays. The map function takes a closure parameter that tells it what to do to each object before adding it to the new array. First, I will use “for in loop” for exactly the same example so that you can see how practical to use Map.

Press enter or click to view image in full size

It’s time to use Map function with closures.

Press enter or click to view image in full size

Filter

The filter function creates a new array associated with the limits you put. For example, from the numbers in an array you have, you can create a new array by taking only numbers less than or greater than 20 with the filter function.

Press enter or click to view image in full size

As a result, you can use closures in many ways to solve so many problems. I highly recommend you to search for more articles and examples to learn closures better because you will use them a lot.

--

--