How Do You Write Closures in Swift?

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey what is going on guys welcome back to the channel hope you guys are enjoying your day so far and I hope you're ready to learn a little bit more about the Swift programming language in today's video now what I'm gonna go over today is how to use something called a closure inside of our iOS applications and what exactly is a closure and how do we write them well for a lot of you guys that have been programming in iOS for a while closures are kind of what you guys used to perform completion handlers for things like URL session and loading data from some kind of external service the way I'm going to illustrate exactly how to write a closure in today's video is to go over a very simple algorithm on how to filter a set of numbers given some kind of predicate or condition all right so let me illustrate this by using playgrounds right here and I have nothing written in my playground so far so I want to write out this function here that I would like you guys to kind of think about how to implement so this function will be called the filter and it's going to be filter greater than value right here and the value is going to be an integer like that and this function also takes in another parameter which will be numbers and this number is just a list of integers in seven array and at the very end I want you guys to return the list of integers that kind of meet this condition right here all right so pretty good stuff and as you can already tell at the very bottom we have some syntactical errors and the way we want to fix this is just simply return an empty array for now all right so that makes the error go away which means that I can go ahead and call this function filter greater than value and let's say I have a value of three and a list of numbers of one two three four five and perhaps ten at the very end okay so the filtering function right here should filter this entire set of numbers and anything that's greater than three I should get back inside of my lists so let's say let filtered list equals all of that stuff on the right and I should get a list of four five and ten all right so how exactly do I do this well let's just simply use a for loop to iterate through all of my numbers and just print them out one at a time so for num inside of all of my numbers within that list let's print out what each and every number is alright so doing so I get one two three four five and ten at the very bottom which is pretty good and how do we check whether or not a value is greater than another value well this is very simple if you just use an if statement so if let's see num is greater than value I will print out num so let me just paste that in there get this spacing correct and now the function will run trying to get me this filter at list and you see I am printing out now four five and ten so that's one step closer to where I want to be and filter list right now if you just print it out you'll see that it is an empty list because we're just returning this empty list right here and you should be able to see something so you see this right here of the empty list and now the final step is to create some kind of variable for me to hold on to all of the numbers that kind of meet this condition of greater than value so let's just go up here and say a VAR you know perhaps filtered set of numbers equals something so there's something I'm going to initialize it as an empty array of integers through this cent like that now I'm going to also return this and because it starts off as an empty list the logic that we had before is exactly the same and then all we're missing is instead of just printing out the number I'm going to say filtered set of numbers dot append new element and simply pump in the value of num which in this case will be the 4 the 5 and the 10 so pretty simple and pretty straightforward and I expect most of you guys to be able to do this and going through some of these simple exercises really helps everyone to learn how how to write out some of the code for you know the syntax of Swift so hopefully this helps you out and let's say I changed this to perhaps a 1 I'll get 2 3 4 5 and 10 inside of my list of filter numbers all right so this is kind of the first part of the video today and I would like to kind of show you guys how to use closures so that I can improve this function and what I really want to do is instead of having this kind of hard-coded function of checking for something like greater than a value what if I wanted to just have some kind of condition instead of greater than value so what I mean is let's say I want to check this list right here for all values that are less than 5 right that means that I have to change this to a 5 and then I have to go inside of this function here and just modify this greater than to be a less than like that and then at the very end my filter list will be 1 2 3 and 4 all values are kind of less than 5 now one thing that I failed to do is to change the function name to be less than value so you would go ahead and do that filter lesser than value change that as well and now everything is ok so this means that every time I want to modify the kind of the logic inside of this function of filtering I have to modify this thing here and also the function name so a better way of doing this is to introduce something called a closure inside of your code so that you can just pass in some kind of predicate and then allow the predicate to perform the condition checking logic which is this line number on line number eight so how do I do that well let's do this again by writing a new function and I'm going to spell it out completely with a long function name of a filter with predicate closure and you probably don't want to name your functions as long as this but I'm just doing this for the examples sake so let's say what exactly is this predicate closure well let's just call this parameter a closure and this closure parameter for this function is going to be a function itself so what I mean is it's going to take in an int and it's going to return a boolean like that and just like the example up here it's also going to take in a list of numbers via this array of integers like the bat and then at the very end it's also going to return some kind of integer array and because this is requiring us to return an int array let's just go ahead and return a blank array like so all right so pretty good stuff and that error should go in and now let me comment out that and let's say I want to perform the exact same logic as filtering lesser than value of 5 or greater than 3 you can now do this by saying let filtered list equals filter with predicate closure and what is this closure well you can just hit enter and you'll get the autocomplete to fill out some of this code for you and for the integer let's just say it's num and for the code I'm going to return some kind of boolean value because that's what I've specified as my boolean or my return for the closure I'm going to say num then five and then for the numbers right here and the numbers let me just copy over this entire rate and pump that in there and let's just print out what filtered list is like so alright so hopefully this stuff should work but because I'm not applying any of that logic inside of this function yet I still get an empty array because that's all I am returning so now the question is how do i how do I even apply the logic of this closure well it is exactly the same as it is above so let's just write out the for loop first so for a number in numbers I need to form some kind of check so perform some condition check here and the way we perform the check is just say if closure if this closure right here using some kind of integer which is the number inside of my for loop like that if that returns a boolean value we will append it into some kind of empty array of numbers so let's just copy or perhaps type this out again and filtered numbers equals an empty list of numbers instead of here and we'll say filtered numbers dot append some kind of new elements and we'll just append the number all right and then finally we'll just return the filtered numbers which is this starting array of numbers right there and at the very end we see that the print the console is printing out one two three and four which means that it's kind of meeting this requirement of number less than five so let's say I change this to number less than twenty it's going to filter this entire list and all the numbers that are less than twenty are going to end up inside of my filtered list of numbers so let's kind of go over how this works one more time so the closure that we're passing into this entire function is this closure parameter that we later on use right here so every time we're iterating through our list of numbers we have number and then we apply that closure which is going to apply this logic right here of checking if the number is less than 20 so based on that logic we can simply make a modification here to get other predicates or other conditions to match the filtering that we want to apply so a number greater than two which means that I get three four five and ten so that's kind of how closures work and I want to show you guys how to write out closures so that you don't have to pass in this strange bit of syntax right here and this is kind of the power of the closure syntax and it makes it so that you can write out your filtering functions as a separate function and then just apply it inside of your function call so that might sound confusing so let me show you exactly what I mean through a little bit of code here so let's say function and let's say greater than three this is my closure function that I'll use later on and this function takes in some kind of number which is an int and then I'll just return a boolean like that and then now what I want to do is just say return a value greater than three so just value here than three is what this function is supposed to do and so I'm going to now say let's see if I just comment that guy out and let's say let filtered list equals filter with predicate closure and this guy I'm going to actually use the function itself because it is a function that takes in an int and returns a boolean I can just paste that in there and for the list of numbers let's give it a 10 5 1 or 1 and 2 and 0 okay so what does this really give me let me just paste that up there I get the values of ten and five inside of this filter list right here and all of these values are greater than three which means that the closure is working properly so what else can I do well I can specify another function perhaps function divisible by five like that let's just use the the word instead of the number and this also takes in an int and returns a boolean so division by five anything that is divisible by five you can just say return value modulus Phi is equal equals zero which for those of you guys are are unfamiliar with modulus this pretty much gives you the remainder of the division of value at five and if the remainder is zero that means is divisible by this number right here so let's just do this one more time let me comment out the initial call and let filtered list equal filter with predicate and I'm going to use divisible by 5 as my closure function and let's just use a 20 31 2 let's see 9 and 15 and it should give me 20 30 and some of these other functions there are numbers right here okay so what am I missing I think I need to add it edie for filter and list and I'll get my function call to call successfully on this set of numbers using divisible by 5 getting me 20 30 and 50 down here so again the advantage of using a function and using closures like this is you can spell out exactly what your filtering functions are and what your predicate what it's supposed to do it's not a separate function so that you can just pass it directly inside of your other function calls and this way you can compose other functions with your filter functions to make your program does bit more robust all right so that's going to wrap it up for today's video hopefully you enjoyed this a very short lesson on how to write closures how to use them and also how to apply the closure logic within another function and so the reason why closures are important is because if you implement them correctly inside of your applications and makes your program much more robust and very easy to maintain as well and sometimes you'll even get this question inside of an interview and the interviewer might want to kind of ask you how to implement a closure on your own so hopefully you enjoyed today's video if you want to download the source code for today's playground project you can find the link it down in the description below and hopefully you guys give this video a thumbs up and subscribe to this channel until next time keep on coding guys and I'll see you in the next video bye-bye
Info
Channel: Lets Build That App
Views: 101,804
Rating: undefined out of 5
Keywords: ios, swift, development, tutorial, learn, xcode, programming, code, closures, closure, anonymous functions, completion handler
Id: fVF_tNcIhfc
Channel Id: undefined
Length: 15min 55sec (955 seconds)
Published: Wed Aug 30 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.