iOS Dev 20: How to create and use Closures in iOS | Swift 5, XCode 11

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi guys welcome to another video my name is emmanuel and uh today we're gonna learn about a very important topic we've actually used it but probably didn't know that's closures so today we're gonna see how to use it and some of the benefits and you know the likes and if you haven't already please just take a second just a teeny second and click the subscribe button turn on notification just so you can get notified on new videos all right so let's jump right in so create a new project and i'm currently using the playground the first thing i want us to know is that closures and functions are the same thing now the only difference is the syntax now i'm going to show you what a function looks like in case you've forgotten and we're going to see how to convert a function to a closure so let's create a function to add two numbers i'm going to call this add it's going to take two parameters um let's say num1 as an integer and the second one is num2 as oh an integer as well now this is going to return an integer also all right so this is a typical function and the function is going to do what return num1 plus num2 simple enough so we can actually just print out our edition so say add and pass in two numbers let's say 5 and 10. what is the answer all right 15. now that works very well so let's see how this can be converted to a closure so we're going to do is right here we're going to create a closure now we can actually assign this to a constant or variable depending so i'm going to create a constant called add right now this is going to be equal to curly brace open and close i'm going to pass in two parameters how do we do that open your bracket pass in your num1 as an integer and secondly your num 2 as an integer now this is going to return an integer as well next we need to use a keyword in and simply say return num1 plus num2 all right now we can actually just comment this guy out and see if it does the same thing so we can run this program and of course we need to remove this or add the label whichever one we please or choose i'm just going to remove this and run it 15 and if you think it was staged let's actually change the value 50 and this should return what 60 right works well so this is a closure it does the same thing as a function just a different syntax now we can actually modify this to act in different other ways and we're going to see how that is done now so one way we can actually rewrite this is to take all of these out of here and then just pass it here to simply say the add variable should be of this type so we're just going to get rid of this label and this as well and right here we can simply just pass in the names of the variable or the label of the variable so here we can say num1 right in here and num2 and the same way we have access to the num1 and num2 values so we can run this we're going to get the same outputs now another thing we can do is we can actually choose to remove this and access the values by using the dollar sign and the index of the value of the parameter so i'm going to say zero just to get the first index of the first parameter and index one to get the second one all right i'm not gonna need the in any longer you can simply just return dollar sign zero plus dollar sign one and if we run this we will get the same value all right how cool is that now the next thing i want to show you about closures is you can actually add closures to an array or a dictionary and to do that we're going to say let's so we're going to create a an array and we're going to say let um funcs equal it's going to create an array and then we're going to pass in closures so i can simply pass an add right here like that and you can actually add more closures to your array and if we hold down option and click on the func you can see that the type is an array of closure that accepts two integers and returns one integer right now we can actually add more but the thing is they have to be of the same type right it's very important so if we were to create another one and say maybe sub and here we're gonna pass in a minus and if we go ahead to add a sub here this is gonna work fine but if we decided to change the type of any of them so let's say we change this to a double and a double like this now this is going to throw an error because the types don't match so this is end okay so yeah it's just basically saying that they don't match so this is an end and returning an end while the other one is double double returning double so how do you fix that simply pass in closure of the same type or expect the same parameter and all of those things all right so we can actually use this basically the same way we have this one being used i can actually um say let add funk just have a different name and this is going to be funks and we're guessing the first index because we know that this returns uh closure to perform addition right so what i'm gonna do is say add func and i'm gonna pass in two values one and two i expect that the answer should be three all right so i'm gonna run this and we should see three right here all right so it works well now we can actually try the same thing but this time we're gonna do for subtraction so let's copy this and over here we're gonna do funks one because we know that one index one is for subtraction and pass in sub here then we're gonna use sub func and pass in um let's say five and three so we expect that the output should be two and that's exactly what it is so you can see that we've actually used our past closures in an array you can decide to do the same thing to a dictionary so if i wanted to convert this to a dictionary you can simply just say uh pass in a string and say add like this and say sub like this and it's still going to work fine and we can actually access this by passing in add and pass in sub and everything should work as expected right it's the problem yeah so this since this is a dictionary you have to force this or safe on a wrap just so that you're sure that the value exists and you can see we have three we have two all right how cool is that now the next thing we're going to see is how to pass a closure as a parameter to a function now yes that is possible so let's see how that is done let me just get rid of all of these for now and i'm going to create a function and this function is going to take in two parameters and we can simply just call it let's call it at i don't know why i always use add so um we're going to pass in num1 this is going to be of type end and i'm going to pass in num2 as type int as well but this time it's going to take a completion or a closure right and uh as the third argument so what i'm going to do is say uh results so i'm going to call this result and this is going to be a closure that passes in let's say two let's say two results so the addition and the subtraction so let's basically just change this to um operation or opera right that's cool now we're going to be returning anything or the closure isn't going to be returning anything so yep we have our function so the next thing we're going to do is to actually use our closure from within this function so to do that as we've been doing we're going to say results which is the name of the closure i'm going to pass in two parameters the first one i want to pass in is the addition of the two numbers that were passed in so i'm going to say let add be equal to num1 plus num 2 then the second one is going to be subtract right and this is going to be minus all right now we're going to call the closure we're going to pass in the addition as the first value and um subtraction as the second so this is going to execute our closure now how do we call this function very simple all we need to do is say oprah operator prop like that and you can see right here expects the num1 we're going to pass in 10 num 2 we're going to pass in um 5. i'll see something different 60 61 i don't know 11. now for the result this is where we're going to pass in our closure so just press enter you can see our closure is already created like that and this as you know from here is our edition so you can simply just pass in add or edition if you want and the second one is going to be subtraction just like that and at this point we can actually just print the addition is what is the addition my man we're gonna do the same thing for subtraction awesome so let's go ahead and run this and see if it works fine beautiful so um 10 plus 11 is 21 10 minus 11 is minus one right so you can see that we actually passed in a closure to our function and if you notice something it actually removed this result label and it's actually known as a trailing closure so the thing is rather than having this as like passing the label like this and then having your um closure created like edition and everything written all over here you can actually just uh sort of like shorthand it to just open your closure like that and just get rid of the label right so it's just something that is possible in case you want it all right so now we understand how to pass a closure into a function there's another thing closures can do and that is to return closure you can return a closure out of a closure or function all right so that's what we're going to be demonstrating right now all right now we're going to create a function and this function we can call this um what should we call this say level right level or game i don't know let's call it game and inside our game we're gonna have something called so basically what we want to do is whenever you get to the next level you want to increase the multiplier or difficulty all right so right here we're going to have a variable that holds or keeps track of the difficulty level so i'm going to say var and i'm going to call this multiplier if i got the spelling and initially it's going to be zero so it's zero from the beginning now we're going to create a closure right here and this closure is going to increase the value of the multiplier so what i'm going to do is say let and we're going to call this increase for i'll know what reason and this is going to be a closure as you know and disclosure is not going to be accepting any arguments but it's going to return an integer right and the integer is going to be returning is our multiplier plus 1. so how do we do that something's going to say return multiplier plus equals one and actually we need to add an in right here and we need to move this guy to the next line and say return multiplier so basically what we're doing here is we're saying increase the value of multiplier by one and then return the value out of this increase function now right here we want to return our closure out of this function right that's what we're trying to demonstrate so right here what i'm going to do is say return and we're going to be returning a value of type or closure of type um doesn't take any arguments and it's going to return an end so this is the type of variable that we're going to be returning from this function so how do we do that we simply just say return and increase which is the name of the closure all right and everything looks very pretty very good now how do we use this i can go ahead and create a new um uh what do you call this a new variable or a constant rather i'm going to call this game one and this is because i want to demonstrate something else so now this is going to be game so we're simply gonna create or call the function game which is gonna return a closure right that is assigned to game one now if i execute the function game one like that's remember it's a closure if i run this you're gonna see that the value of game one is one let's actually go ahead and print this so it goes to the console so i'm gonna say print and run this again i'm gonna see game one right and that's good now this brings us to the second thing i'd like to or the second uh i don't know second thing i wanted to point out about um closures they actually capture variables in their surrounding or environment context what i mean is now we have multiplier here and the value is zero right now when we executed this increase function which is from here all this increase closure from here game one and executing this it actually increased the value of the multiplier right now if i were to run this exact line of code again what do you think is going to happen i'm sure you guess right it the new value is 2. this is because it took the value of it took everything all of the variables within this context and sort of like created a copy of that so whenever you perform operations or whatever calculations and manipulations within this particular context or closure it's only going to be affected there now this is what i mean if i went i had to create a second one game two and we're creating another closure or getting another closure if i were to run this and print the value of game two instead can you guess i'm sure he guessed right so the value or the answer result whatever is one now this is because this is a separate or a different closure all right so just keep that in mind whenever you make modifications within this it's going to affect the variables in that context right so uh that's basically the next thing i wanted to talk to you about and don't get me wrong closures are very powerful very powerful we've used them in different areas of our code and we're going to use them subsequently so um there's actually something else you can do with closures that actually do in my project so you can actually use closures to send information from one view controller to another right now i'm gonna leave that to you try and figure it out if you have any problems then feel free to leave a comment and we can tackle it together right uh meanwhile uh this actually brings us the end of this video and uh if you have any questions again go ahead put it down there in the comment section and i will explain as best i can meanwhile if you haven't subscribed already please just take a second again click on the subscribe button and see you guys in the next video [Music]
Info
Channel: Emmanuel Okwara
Views: 2,078
Rating: undefined out of 5
Keywords:
Id: roE-PvtX7aQ
Channel Id: undefined
Length: 18min 4sec (1084 seconds)
Published: Sat Oct 10 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.