Introduction to Closures in Swift 4

Jayven N
iOS App Development
3 min readFeb 20, 2017

You’ve seen it. Let’s talk about it.

Jump. You never know.

Apple, what are closures?

Closures are self-contained blocks of functionality that can be passed around and used in your code.

— Apple

Don’t worry if that make no sense. At first, it probably sounds like:

Closures are self-contained bunch of words passed around like a damn basketball. In case you don’t know, there happen to be a basketball court in the Playground. Go check it out.

— Apple

PURPOSE

Introduce closures on what, how, and where.

WHAT ARE CLOSURES

Closures take one of three forms:

  1. Global functions
  2. Nested functions
  3. Closures expression

The first two are special cases of closures. If you don’t know how functions work, I have an article for you here:

Master Functions in Swift 3

When we talk about closures in Swift, we often refer to closure expressions. If there’s one thing to help you remember what closures are, this is it:

Closures are headless functions.

Closures are functions without the func keyword and the function name. They are also known as anonymous functions. They can’t be called by themselves like a function because they have no names. They are used in conjunction.

CLOSURES SYNTAX

Image from Apple

Closures are encouraged due to its simplicity in syntax.

FUNCTION VS CLOSURE

Here are the key differences to remember between a function and a closure:

FUNCTION

  • has a name
  • has func keyword
  • has no in keyword

CLOSURE

  • has no name
  • has no func keyword
  • has in keyword

DEFINE

Let’s see how we define a function versus a closure.

func giveAFunc() { }var giveNoFunc = { () -> () in }

Note: Our closure begins with { and ends with }. We store the closure inside of a variable so we can call it.

CALL

Let’s call a function, then call a closure.

giveAFunc()giveNoFunc()

The way we call it is identical. Now that we have seen how closures compare to functions. Let’s see how we can transform from a function to a closure.

FUNCTION TO CLOSURE

Let’s transform sayHello to a closure.

//Soon to become a closure
func sayHello(name: String) -> String {
return "Hello \(name)"
}

STEPS TO BECOME A CLOSURE

  1. Remove curly braces
func sayHello(name: String) -> String
return "Hello \(name)"

2. Add in keyword between argument list and function body

func sayHello(name: String) -> String in
return "Hello \(name)"

3. Remove func keyword and function name

(name: String) -> String in
return "Hello \(name)"

4. Surround with curly braces

{ (name: String) -> String in
return "Hello \(name)"
}

That’s it. Done.

You can then assign the closure to a variable and call it like a function.

var sayHello = { (name: String) -> String in
return "Hello \(name)"
}
sayHello("Jayven")

WHERE ARE CLOSURES

A topic that people often leave out when talking about closures is where do we actually find closures? Well here is the answer. Everywhere. Closures are deeply rooted within the Swift language. Most common use cases include completion blocks/callbacks, higher order functions, and syntactic sugars.

WRAP UP

We have introduced closures. What they are. What they look like. How they are formed. And where they are. Congratulations for making it this far.

Introduction, introduced.

LAST REMARKS

I hope you have enjoyed and learned something valuable from my article. If you have then let me know by hitting that ❤ button and follow me on Medium. Also, share this article so that your circle can make some knowledge gains too.

For those interested, here is my LinkedIn.

Lastly if you have any comment, question, or recommendation, feel free to drop them below. Tell me what you want to learn next!

Feel free to check out my recommended articles:

Master Functions in Swift 3

Introduction to API.AI

API.AI in Swift 3

--

--