paint-brush
Basic Closures in Swift for newbies.by@farhansyed
198 reads

Basic Closures in Swift for newbies.

by Farhan SyedApril 13th, 2017
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

A lot of beginners have trouble with closures starting out so I thought I’d try to show it my way to hopefully help anyone out.
featured image - Basic Closures in Swift for newbies.
Farhan Syed HackerNoon profile picture

A lot of beginners have trouble with closures starting out so I thought I’d try to show it my way to hopefully help anyone out.

First, I’d like for you if you don’t already to grasp the foundation of functions in Swift. You can check out my post here on it, then come back here once you have functions locked down.

Let’s get into talking about closures.

Closures are functions without the func and the function name.

You may ask why even do closures?

Well the reason why closures are encouraged is due to the simplicity of the syntax it provides.

Let’s look at both.

// function
func someFunction() {}

// closure
var someClosure = { () -> () in }

To call closures, it’s exactly the same as functions.

someFunction()
someClosure()

Let’s now do a simple function then change it to a closure.

Function Way.

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

// Call Function
saySomething(name: "Farhan")

Closure Way.

Here’s a basic guideline you can follow:

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

// Call Closure
saySomething("Farhan")

Closures begin with a { and end with a }.

Like functions, closures have similar structure.

But in closures make sure you put in after your return type (In this case String).

Quick Tip!

To make it even shorter, you can get rid of the return type -> String .

Swift is smart, it has a feature called Type Inference to know the return type is a String so why type more than we need to?

So it would look like this:

var saySomething = {(name: String) in
  return "Hello \(name)"
}

That’s it!

Let’s do another example to make sure we get the hang of it.

Let’s first create a func:

func returnName(first:String, last:String) -> String {
 return "\(first) \(last)"
}

returnName(first: "Farhan", last: "Syed")

Let’s translate this into a closure:

var returnName = {(first: String, last: String) in
  return “\(first) \(last)”
}

returnName(“Farhan”, “Syed”)

I hope by now you understand the foundation of closures as there a pretty big part of Swift, you might even be using them without knowing.

I will also be covering shorthand in another article coming up soon so stay tuned!

The End.

I hope this was easy concept to grasp as I tried to explain it simply. If you had any questions or suggestions feel free to let me know.

If you got stuck during the guide, I’ll have sample code up on my GitHub for you to check against yours to see where your going wrong. Or simply let me know and I’ll do my best to help. 😊

Thanks again for reading!