BSM

Mar 28 2020

Featuring:

HWS+

HWS+ is an all new service from Hacking With Swift. As of writing, it has 3 courses, which each have a few sections. Until WWDC, HackingWithSwift will publish a new section to one of the 3 courses every day, which afterwards will be every few days.
I can say, after watching all the videos that are released, that they are from really high quality(not only the resolution, also the content🙂!), and are really recommended if you want to level up in your swift and iOS development skills.
Once you are there, be sure to check out the other courses they have here


Closure Parameters

Basic parameters

The Closures in the examples of the previous articles, don’t have parameters so they only “show” data, they don’t modify it. To modify data within a function, we use parameters. So too with closures, if we don’t want to have static closures and we want to be able to modify closures based on the user input, we will need to add parameters.

To add parameters in a Closure, we would do the following: after the opening brace, you write parentheses. In the parentheses, we write our parameters. The parameters of closures work the same as functions. after the parameters and the closing parentheses, we write the keyword in which is just syntax for closures with parameters.

Example:

Let’s start by creating a regular closure that just prints out a static message.

let yourName = {
    print("hello, the author's name is BDev")
}

If we would decide to use that, and then we want to print out more times the author’s name, but each a different author name, then it’s kind of stupid that we have to copy-paste that code. Because if we would want to have 10 or 50 names, our files would get way too long. In that case, we could use parameters so we don’t have to copy-past all that code and to make out files much smaller which helps for maintaining the code and fixing bugs.

Adding parameters to a closure

As I explained before, we will need to write after the opening brace, our parentheses. Inside the parentheses, we write our parameters. In this case, we write name: String which defines the name parameters as a string. After the parentheses we write, as I said before, the in keyword. after the keyword we tap enter and we’re inside the closure. Now where inside the closure, we could print out our message. In our case, we want to say hello to the user and ask him how he is doing:

let yourName = { (name: String) in
    print("hello\(name), how are you?")
}

So, what we did with this code is that we declare the closure to except parameters. We gave the parameter the name name because that is what we want.

As I wrote in my previous post about the basics of closures, you call a closure the same as a function, that is not the case with closures that accept parameters. With closures, you don't write the parameter names. Instead, you just write this:

yourName("BDev")

I passed BDev in the name parameter. So when we run our code, this will be printed out:

"hello BDev, how are you?"

Multiple parameters

As functions, closures can also have multiple parameters. You do that the same as function. After the first parameter, you should write a comma and then you would write the second parameter.

So:

let yourNameAndAge = { (name: String, age: Int) in
    print("Hi \(name), you'r \(age) years old.")
}

What we did here is we defined our closure so that it will take 2 parameters. The first one is the name parameters, and the second one is the age parameter. Inside the closure, we have a single print statement that prints out hi \(name), you're \(age) years old. You would call it the same as a regular closure that accepts only a single parameter, but you’ll enter the second parameter separated with a comma.

yourNameAndAge("BDev", 1111)

And that will print this out:

"Hi BDev, you'r 1111 years old."

So, this was quite easy, wasn’t it? I hope you understand how to use parameters in closures, and that you’ll be able to use it in your own code.

Don’t forget that you can email me at questions@bdev-code.nl for any questions, feedback or if you just wanted to say hi.