Clearing Up the Confusing Closures Concept in Swift
Understand closures through examples
Closures are very essential in the daily life of a programmer. They represent some block of code that you can assign to a variable. In addition, that piece of code can be passed to a function and used as an action that should be executed after another action of a function.
It may seem a bit confusing right now, but I will try to make everything clear within the article.
Without further ado, let’s look through the syntax of a closure:
This is the simplest example of a closure. As you can see, we created a constant variable named greetings
and assigned it to the message in curly braces. On the last line, the closure is called. It’s executed by calling birthday()
. Have you already noticed the link between curly braces and closures? If so, congrats! You just learned the basic concept of a closure.
Now, let’s look at the fullest form of a closure and simplify it step by step:
(String) -> ( )
— We specify the type of a closure with oneString
parameter and wait for it to return nothing.(String) -> ( )
is equivalent to(String) -> Void
.(name: String) -> ( )
— We do the same specification of a closure type, except we givename
as our parameter to be passed.- The
in
keyword serves as a border between parameters and the body of a closure. So after thein
keyword, we can write some actions of the closure. - In
greetings(“Mike”)
, we pass theString
of aname
.
The output:
Hello, Mike!
Since we said that specifying the closure type is done twice in our first example, we can easily omit one of the specifications:
In fact, we can also get rid of the parameter’s name
specification and just use $0
shorthand!
Have you noticed that we also didn’t use the in
keyword? This is because we haven’t written any parameter name. Here, $0
refers to the String
parameter.
A few more examples of syntax annotation:
(String, String) -> ()
: Two parameters ofstring
return nothing.(Int, Int) -> (Double)
: Two parameters ofint
returndouble
.
Sorted Method
Let’s go over a built-in Swift closure (sorted
) that sorts some data for you:
Remember: Being able to make it short is good, but making your code more readable for others is much better!
Types of Closures
In Swift, we have different types of closures, such as trailing
, escaping
, autoclosure
, and autoclosure escaping
.
Trailing closures
When the closure is the last parameter in the function, the closure can be written outside the function’s parentheses and the parameter’s label can be omitted. This is called a trailing function:
Escaping closures
A closure is called to escape
when it is executed after the function returns. You just need to declare the @escaping
keyword before the parameter type to point out that the closure is allowed to escape:
Autoclosure
Autoclosure
executes automatically before the function returns. In order to use it, you need to declare the @autoclosure
keyword before the parameter type:
Autoclosure escaping
As you might already understand, autoclosure escaping
is a symbiosis of two closures. If you want an autoclosure to be allowed to escape from a function, use both the @autoclosure
and @escaping
attributes:
Wrapping Up
Closures are very important and a base functionality in Swift. I hope this article gave you a better understanding of what they are and how to use them.
Interested in other relative topics? Feel free to visit my other relevant articles:
If you have any criticisms, questions, or suggestions, feel free to post them in the comments section below!
Thanks for reading.