In business and we'll be talking about closures on completion blocks closures closures a very powerful
feature of excoriation Schrift and we use a similar thing in Objective C called a block.
And what they do for us is closures are like a function without a name.
And they help us in situations such as when you want to pass a function as an argument inside another
function that being used in so many different places the school head into its quote and I'm going to
start a new project.
My course command line tool.
And I'm going to call this one armed soldiers safe and let me maximize profit.
OK.
So here the first thing they're going to do is just to get a sense of how you declare and you closure.
Imagine if you had a function call.
This is a function doesn't take any arguments and doesn't have any return types.
So this is the simplest way we could introduce a function his name nor are humans nor return types on
this the body of our function.
If I wanted to convert the same thing into a closure I would go ahead and say variable This is closure
and that closure would become something like this.
You open the curly brackets first then you talk about your arguments and then you talk about what ever
that's going to happen between that body.
So that's what it means.
It essentially means I have a closure that takes some time.
Humans are a retentions or something.
As long as something else is valid as long as the are thing that you want is being met.
Now this might seem a little bit complicated.
So let's go ahead and actually write one that helps us doing something.
I'm going to go ahead and say well how the value of the sets say hi closure to survival is going to
say high.
And then I'm going to open Carol-Lee markets.
I'm going to say my argument is him name which is of the tie the strange.
And then for that I wanted written another string in the return.
What would you like to be with.
I like to say hi.
And then whatever valuable I have interests.
So you're articulate this is the function that has an input argument and unharness a string out on returns
that if I wanted to convert that function in to hear if I wanted to say Fine say hi function I would
be Diest I would be in name just like what we have done many many times.
It returns a string and then it does this return date.
So these two are essentially identical.
Now I don't want to use the function I want to use the closure now.
So in the closure I'm going to say I have this and the way I could use it I would save preens.
What you want to print.
Here is the value I want to print.
This closure.
So I'm going to get the closure of say hi closure for a value such as my name because obviously it receives
one argument.
So if Iran does not it should say hi and the name that comes off that in this case we want the space.
Let's see if that is the case.
Ari So so far I've managed to have a closure.
That's the most basic closure that takes in an argument and does something and be understood kind of
the format of it is whenever you want to have a closure you have this format curly brackets humans returns
in.
Now let's go ahead and write another one.
So in this new one I'm willing to see some of the actual power of closures.
I'm going to go ahead and actually I'm going to comment everything out in here so that you know they
don't they don't get mixed down there and I'm printing values.
And here's a little bit of an explanation of what I'm trying to do.
Let's say I have a function on my function.
You don't need to do this because I'm going to change real soon or my function is set my function and
it has an argument as an as an entry to it.
Let's say that argument is numbers all right all values song going to save values right and those values
are an array of integers and then I'm going to do something on these array of values of whatever that
thing is maybe I want to find out which of those are larger than a hundred so larger than 100.
And then I want to have another function that goes ahead and says Well tell me the ones that are on
this farm what I call fun the ones that are smaller than 100 and you can imagine this thing can continue
for ever so many different functions each of them has a different let's call it condition.
So here's the thing I have so many functions that are very similar.
But each time the condition changes.
So what I'm trying to do is I'm trying to add the condition as one of the arguments in here so I can
only have one function that can do so many different things and we can do that using Crozier's.
Let's see how I'm going to manage that when I say give me a function Let's call this one multi multi
purpose function multi per function like a cat on my multi-port function has these things.
First of all it has a set of values that receive.
So let's call it the vowels.
And the vase is going to be an array of integers.
So that's the integer values that we want to do something on them.
But what we want to do and then I'm going to make it a closure.
I'm going to say it's a closure.
And that's just the name of a of a closure name of an argument you can call it whatever you like make
sense to call that closure here.
I'm going to say it's a closure that receives is an integer and returns a boolean.
So let's see what that means.
It means you will have a set of values and you will have another function as an entry for this function.
I'm going to rewrite this once you are done as if it were two separate functions and you would see how
it works.
But for now I'm going to go ahead and say well here's what I want to do.
I want to say for each number in vowels.
This is what you want to do.
You want to say effect closure of that number of each number.
If that was a valid value I don't know just printed form to say print each name.
That's all you're going to do for me right now.
I could change the print to interpret it properly like.
So what is the condition.
What is it going to do.
I know that the numbers might be I don't know 1 2 5 and 5 million 6 million whatever the numbers but
what is the condition that is going to say if that was true do it.
That's something that we can do on the fly as he called the function.
Just go ahead and do it.
And when I say multifunction moral purpose function which is here that numbers I'm going to give with
an array of let's say 100 200.
Seven hundred to twenty five and then the closure is going to say integer in some sort of condition
and I'm going to say call this integer value and then say the condition is this As long as Val is below
300.
So as you can see I called a function with an argument.
Another argument that that other argument of mine is actually a condition itself.
So let's run this and see what we get if you Ronit below 300.
We should get a few of these numbers 100 or 200 225 but maybe one or two.
So either with a different condition I wanted to see a valid larger or equal to 200.
Let's see what we get now.
We should get only the 700 number.
Now if I were to rewrite this whole thing using nested functions it would be something like this.
And I really suggest you don't try this with me just just have a look at it because you're not going
to really use it.
It would be called a nested function a minus that would receive these values as our input arguments
and then it would have a body which inside its body it would have another function called our internal
function that my internal function receives of one value just like here it would receive one integer
value and would give me back a boolean.
And then what does it that that one do.
One says for instance if one is in this case a smaller larger than 100 if it is larger than three hundred
then return true.
Right.
So I may write this a little bit.
I don't really like typing code like this but sometimes because I'm trying to zoom into the screen for
you guys I have two.
So one is if that is that and otherwise else and in the else I'm going to say here it's false.
So there's an internal function that receives a number it is larger than something it returns true otherwise
it is false.
Now within the actual body of function it would say for each value you mean vows.
Do this what should you do.
You should say if the internal function of each vial was true then do what we did before.
Preens this not so in our purines each.
So this would be the longer we're on if I can show you both of them at the same time.
So yes this would be a longer way around a nested set of functions and this would be an easier way around
using a closure that a closure on the fly.
Whenever I'm calling it I can actually decide what do you want to see.
Also here I have only one condition but here I could actually decide about the condition as I go through.
Like I could have this function and I would have another one say multi-purpose function the values or
another set of values and that other set of values.
The closure for it says for any value if Val is smaller than 500 so the same function is being used
at the same time with no changes to the body of the function because in here with no changes to its
body but is doing two different things for us which obviously is a lot simpler than writing nested functions.
Some remove that nested function and well remove that.
And now let's move on to another thing about closures which is called Escaping closures.
So let's see what is an escaping closure escaping closure escaping closures or a particular type of
closures that they get called within the arguments of a function and then they can return back from
the function.
They are primarily used for synchronous processes asynchronous process is essentially a process where
you're reading something off the network and it has two word power of your are there threats out there.
You say user interface thread or computation thread simply because when you make a connection with the
network backend it might take a little while before you get some results.
Let's say it takes half a second or one seconds or even you know if you're on a slow connection it might
take two seconds.
So in that time your app should unfreeze.
So they have to work synchronously.
They have to work parler each other.
And that's you know what we do for situations like that we use a closure that waits for something and
then gets essentially popped back.
So with that in mind let's see how we're going to write an escaping closure and it going here and get
us.
We have a function that is going to read the photos from our backend.
So I'm going to say function the search for talks.
So this is a function that's going to fetch pictures for us from some sort of a back end that we will
see in quite a few sections from now to be honest.
But we can see the process of a closure in here.
So I'm going to say my fecche photos has a link that is going to be a string that will read and refine
it and then we want to have a closure for it.
My closure I'm going to call it completion which is a very common naming convention people code completion.
Troy prope tells them they completed or not.
And I'm going to say my completion has these things has a boolean and has a set A and array of a string
of the names of the photos that it's found.
It doesn't return anything essentially.
So I have a completion closure that returns whether it succeeds or not based on that returns a set of
photos or not.
And this may have made it optional because it's possible that this bullion is false and we have actually
failed in fetching information and as a result these things are nil.
There is nothing in them.
And then I'm going to go ahead and say well this completion is actually escaping us.
K ping.
So it's an escaping closure which will be called later on.
So similar to the other closures that we had a closure inside the body of the function.
It gets called immediately but it doesn't get returned immediately on live here.
That whatever is happening in the van and the bully and or whatever this statement is it must be returned
right then and escaping closure essentially wait for it to be completed and then it gets returned.
And that's essentially the power of it.
So let's see how are we going to use that.
So here I'm going to go ahead and say let's say we have a status.
This is not this is far from us from the network status coming from the network.
So now that we don't have access to a real network I'm going to essentially fake that value.
I'm going to say let her status and I want to make her status to be something that is either true or
it is false.
So I'm going to say Give me an arc for random human or random off to reach this number is either one
or zero simply because a random number below two is either 0 or 1.
And I'm going to say if it is status it means if this was true.
Right.
If it's not those who want to do something I shouldn't have written it like that and else we should
do something else.
So let's see what we have to do.
Let me get myself better spaces in here.
So in the case of else that's quite clear.
We have to say completion.
We have to say completion false and we didn't get any images.
That's exactly what he's expecting us to tell him.
A boolean value and an array of strings which is optional.
And then in case it was true I'm going to say completion.
True.
Yes it succeeded.
And give us some we know of fake photos for it or one photo or two.
So that's what we have in here.
Let me read for my things a little bit again because I need to see everything once again.
So now we have both of these statuses happening and we have our function.
Let's go ahead and actually use it.
I'm going to go ahead and say fecche photos.
I said give me a link.
So I'm going to say fake.
Imagine that was an actual link and then it says what you want to get at the completion of the completion
I want to get the result I want to get our photos Araik.
Let's call it now.
Here is what we do we say if the Rez if resulta was true it means after this event happened that that
is the very long event that we currently don't have access to.
But if that one happened and it was successful we got back all of these and if we had a successful result
then use the photos Araik otherwise or tell us that oh there was an error.
So on accident.
Right.
The other advisors say us do this Corinne's prints error in reading metaphor or something to that effect.
Now obviously right now I'm faking Duvalier's I can't really be sure what happens but you could try
it.
Let me let me go ahead and comment these guys out as well.
So I only have to deal with this one escaping closures for a Synchronoss tasks.
So if you run this now depending on whether that number becomes true or false you either get to see
the photos or we get like here error in reading networth because something happened so we couldn't get
to the results by so that's about that closures escaping closures closures we use them as a way of passing
functions as an argument into another function to avoid having nested functions something I personally
do when you don't really have to do it.
What they really really like it is I don't put my closures within the body of the argument or actually
write them as a separate thing.
So I essentially say type alias under Type early is that they want to call it.
In this case on line closures or anything that you want to call it.
And that becomes the entire Declaration of my closure.
So it says on line closure is that so on and so forth.
And then in my function I have a link of the type of string and I have a compilation of that type on
line closure which is also escaping in the form of that little bit so it's easier to see.
So essentially you managed to put my on line closure inside a separate variable.
Like I said you didn't see this as a very common practice.
I really like it this way simply because I end up using the same closure in so many different places
I might be dealing with it uploading either no photos downloading photos or a lot of times your closure
is just a bully and that says video or something completed or not.
So I know that I'm using that to one closure everywhere.
And if I want to modify they only modify it once so that's about that.
We looked at different types of closures are simple closures just how they correlate to a function.
We looked at how to write a function that receives multiple different you know conditions as a closure
as one of its arguments.
And each time it can give us a different result.
And finally we look now at escaping closures and that's about that.
With that in mind let's move on to our next lesson.