What are Closures in Swift 3? (Part 3)

Wilson Balderrama
Nov 27, 2016 · 3 min read

Continuing with the part 1, part2, this post is going to be the last one regarding Closures and we all know for sure that it can be written an entire book talking only about Closures so I’ve just touched barely the surface.

This time I’m going to talk about:

  • Shorthand Argument Names
  • Trailing Closures

Shorthand Argument Names

One way to iterate an array is using the forEach method that in the following example is going to print its content:

Closures have a feature that allows us to simplify our code that instead of having to access its values through arguments, we could access them using Shorthand arguments names:

You can notice that we have omitted the argument name number even the keyword in, and we are accessing the value provided by the closure through the name: $0 so that our code is reduced and we can concentrate in the most important part of the code.

So even if the closure had more arguments, the shorthand names would be distributed with $0, $1, $2, and so on, you get the idea.

Trailing Closures

Let’s say we have a function called findInteger (don’t worry the source code of the function is at the end) that’ll use an Integer array to search for an element based on a condition:

So in the above code we want to get the first number that satisfies the condition on the closure that is the number sever.

We can see that we are receiving a closure expression on the second argument but sometimes like in this case the expression becomes long and instead we can do this:

The difference is that first we didn’t need to use the argument label closure as part of function call.

And second now the closure expression comes after the function call’s parentheses but it is still an argument of the function.

This way of simplification is called Trailing Closure.

Let’s see another example.

This time we have a method called forEach (source code at the end) on Integer’s extension structure which will allows us to iterate digits on a number like this:

The code is pretty much explanatory itself but the closure is something long.

Let’s apply some Trailing Closure.

So here as the last example (findInteger function) we didn’t put the argument label called closure and also didn’t include the parentheses.

if the closure expression is the only argument as which happens in the forEach method, then we can eliminate the function’s call parentheses too.

Trailing closures come to the rescue when the closure expression is long.

And as I mentioned the source code of the methods used in the examples:

If you find useful the article please help me recommending clicking the green heart outline icon so that I can continue creating more articles, thank you so much!

I want to personally thank you guys Bob and Victor for motivating me to share my Swift stories to our awesome iOS community.

Thanks for reading me!

Wilson Balderrama

Written by

I like to run everyday while listening podcasts or audiobooks, read/write at Medium and of course I try to improve my skills as an iOS Developer everyday.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade