Check out a free preview of the full iOS App Development with Swift course

The "Closure" Lesson is part of the full, iOS App Development with Swift course featured in this preview video. Here's what you'd learn in this lesson:

Maximiliano discusses Swift closures as literal functions, anonymous functions, or lambda expressions in other languages. The data type Void being used to express no return value and the syntax for functions receiving closures as arguments are also covered in this segment.


Transcript from the "Closure" Lesson

>> Closures, what is a closure? What is typically known as a anonymous function, a literal function, or a lambda expression in other languages. Is when you pass as an argument a function or you create a variable with a function inside, or you return a function. You have a function returning another function.

You can use these for callbacks for event management for functional programming, like using map or filter on other languages. In this case, it's called closures. So the type of the closure looks like this. We use the thin arrow, so when you see the thin arrow in a type, it means it's a closure, it's a function.

In parentheses, the arguments, and after the arrow, the return type. And we have a special type, the capital V bold, saying that in this case, it's a function that receives no arguments and returns no argument. Okay, and how you define the content of the function, code block. So equals code block.

So this is in JavaScript, creating a function with a name closure like this, but this is a normal function. This is like creating something like this in JavaScript This is JavaScript, okay, no twist. Just to give you a quick idea, okay? This is how you set up that thing.

If you have arguments, you use the argument for example, here I have an integer. How do you use the argument inside? You have two ways, you can use the dollar sign, the dollar sign will give you a tupple, some kind of a tupple of all the arguments. So if I have to on a string, you have dollar zero,dollar one but most of the time you don't wanna mess with this unless you have only one.

Typically you wanna name them. Okay so for example I have a function here, request data that receives a callback as an argument and what's the type of the callback in receiving a string and returns nothing, okay. So if you wanna name the arguments, okay? You name them, that's weird, this part is weird within the block.

So within the curly braces, you put a name, for example, in this case, this is the item in, so instead of dollar zero, you use item now. Okay, it's complaining because it's an old error I think, because I added an argument, so now he has two arguments, item and our name.

And in, but this is weird because the signature of the closure is inside the code block, which looks weird, okay, it looks I know it looks weird. It's not outside, it's inside, okay, and these are the two names that you will receive from the fabric. How do you call a closure like any function?

You just call it because it's a function. A closure is a function, okay. Here comes the interesting part and we are going to use this a lot in Swift UI. That's why I'm taking some minutes to talk about this. How do you call request data? Request data receives a callback, callback, look at the the color.

Okay, it's the same color as the function name, so we actually need to explicitly say callback. It's the argument label. If you don't want that you use underscore. And now you see the different color name, a different color for the name because it's part of the name it's not part of the name, okay?

Does it make sense? So, here it's asking for a closure, so I'm going to use the code block and it receives a string, to data. In, we'll use data in, okay, it's okay so this is like in JavaScript doing something like this great function or an arrow function if you want,okay.

So I'm trying to give you an example in JavaScript that most of you will we understand this is JavaScript, okay, for that code. Does it make sense? Well, the next part will not make sense the first time you'll see that instead of doing this, so this is actually pretty common in JavaScript.

And it's typically a problem in JavaScript that you're closing code block, you're closing a curly brace and also a parenthesis. And sometimes you get messed with this. Okay, the parentheses where these parentheses come from. So well actually what you can do when the last argument of a function is a closure, we start with that sample first.

You can get rid of the parentheses and end up the parentheses before the code block and you can get rid of the argument label as well. And if there are no more arguments, you can even get rid of the parentheses. They say [SOUND] some of you are doing this at this point.

>> [LAU GH].
>> For some reason, we think that if we tilt the head, we will understand. So what's going on here, in fact let me show you this, if I use the order competition so I start typing and you see a press ENTER I'm not sure if you realize there is something in blue black background there.

That's actually placeholder, if you press return here or the enter key, something magic will happen. It will actually replace the call saying, hey, we're in Swift here. So if you have an argument that is a closure, we can move that argument out of the function call. And then you just need to name In the argument and pull the code inside.

so again this is new for most of you because when you look at this you don't see a function call,, no parentheses, no function calls. Well, yeah we do have a function call here,, this is and I know you're not believing me at this point. This is to increase the clarity of our code, to reduce the amount of parentheses and code blocks and problems that we have balancing those parentheses.

That's the idea, because it's a common pattern to actually call a function that receives a function. So, instead of doing for example if I have an array, I'm gonna map that array or I wanna filter that array. In JavaScript, you say, okay,, filter receive data and then you do something like this.

So you end up with this curly brace parenthesis problem. Well here, is a filter and then they dine. But I'm still executing a filter function and I'm still passing an argument, okay? Hey, but it's complaining because it needs a ball and it's gonna need to return something. This is a compiled language, so I must comply with all the data types.

Okay, does it make sense?
>> Can you go over the in one more time, cuz I had a different interpretation before coming into this class. But can you explain in again?
>> Explain, this one, this part?
>> The in So if you go up more probably at the very basic level the like 12, I think that's when you started introducing in.

>> the in, okay, okay. So, yeah sure. So let me get rid of this for a minute. So when you have a closure, so I have a variable that is going to be my function and I say that the type is a closure that receives an integer and our integer like a coordinate on a Boolean.

Okay, so then how can I set the value for that variable? It's a code block because it's actually a function. And how can I set names for these three arguments, well before, this, by the way, I forgot the return type, sorry. So before executing some code, I say I call x, y, I'm basically all in, on the code in, it's just a separator between the name of your parameter names, the name of your variables, your argument variables and the code.

So in just, it's just that. Why it's in? I don't know but it's, okay, you have this input and you have this code and in is what separates both parts of your code.
>> Okay, I mean, is it a better way to explain it as you're just mapping your variables to.

>> It's yeah, it's actually it's mapping the arguments into local variable names. Yeah, we don't use the in other places,, okay, it's just here.
>> Thank you.
>> You're welcome. Okay, we will use these a lot during the rest of the day, okay. So no worries I will repeat these shortcuts, okay, that removes parentheses from function names and it seems kind of weird.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now