1 00:00:00,240 --> 00:00:06,570 In business and we'll be talking about closures on completion blocks closures closures a very powerful 2 00:00:06,570 --> 00:00:12,900 feature of excoriation Schrift and we use a similar thing in Objective C called a block. 3 00:00:12,930 --> 00:00:18,700 And what they do for us is closures are like a function without a name. 4 00:00:18,880 --> 00:00:25,410 And they help us in situations such as when you want to pass a function as an argument inside another 5 00:00:25,410 --> 00:00:30,600 function that being used in so many different places the school head into its quote and I'm going to 6 00:00:30,600 --> 00:00:33,010 start a new project. 7 00:00:33,150 --> 00:00:35,440 My course command line tool. 8 00:00:35,830 --> 00:00:47,290 And I'm going to call this one armed soldiers safe and let me maximize profit. 9 00:00:47,670 --> 00:00:48,180 OK. 10 00:00:48,180 --> 00:00:55,640 So here the first thing they're going to do is just to get a sense of how you declare and you closure. 11 00:00:55,770 --> 00:00:58,050 Imagine if you had a function call. 12 00:00:58,080 --> 00:01:05,260 This is a function doesn't take any arguments and doesn't have any return types. 13 00:01:05,280 --> 00:01:12,510 So this is the simplest way we could introduce a function his name nor are humans nor return types on 14 00:01:12,510 --> 00:01:14,460 this the body of our function. 15 00:01:14,520 --> 00:01:22,080 If I wanted to convert the same thing into a closure I would go ahead and say variable This is closure 16 00:01:22,380 --> 00:01:25,980 and that closure would become something like this. 17 00:01:25,980 --> 00:01:33,500 You open the curly brackets first then you talk about your arguments and then you talk about what ever 18 00:01:33,510 --> 00:01:37,560 that's going to happen between that body. 19 00:01:37,560 --> 00:01:38,640 So that's what it means. 20 00:01:38,640 --> 00:01:41,710 It essentially means I have a closure that takes some time. 21 00:01:41,790 --> 00:01:44,260 Humans are a retentions or something. 22 00:01:44,310 --> 00:01:51,410 As long as something else is valid as long as the are thing that you want is being met. 23 00:01:51,450 --> 00:01:53,900 Now this might seem a little bit complicated. 24 00:01:53,910 --> 00:01:58,040 So let's go ahead and actually write one that helps us doing something. 25 00:01:58,200 --> 00:02:06,690 I'm going to go ahead and say well how the value of the sets say hi closure to survival is going to 26 00:02:06,690 --> 00:02:07,420 say high. 27 00:02:07,530 --> 00:02:10,050 And then I'm going to open Carol-Lee markets. 28 00:02:10,050 --> 00:02:16,820 I'm going to say my argument is him name which is of the tie the strange. 29 00:02:17,100 --> 00:02:23,800 And then for that I wanted written another string in the return. 30 00:02:23,820 --> 00:02:25,330 What would you like to be with. 31 00:02:25,440 --> 00:02:27,550 I like to say hi. 32 00:02:28,080 --> 00:02:32,090 And then whatever valuable I have interests. 33 00:02:32,370 --> 00:02:39,930 So you're articulate this is the function that has an input argument and unharness a string out on returns 34 00:02:39,930 --> 00:02:48,270 that if I wanted to convert that function in to hear if I wanted to say Fine say hi function I would 35 00:02:48,270 --> 00:02:53,560 be Diest I would be in name just like what we have done many many times. 36 00:02:53,680 --> 00:02:59,250 It returns a string and then it does this return date. 37 00:02:59,400 --> 00:03:02,000 So these two are essentially identical. 38 00:03:02,100 --> 00:03:05,130 Now I don't want to use the function I want to use the closure now. 39 00:03:05,160 --> 00:03:10,370 So in the closure I'm going to say I have this and the way I could use it I would save preens. 40 00:03:10,410 --> 00:03:11,490 What you want to print. 41 00:03:11,490 --> 00:03:13,440 Here is the value I want to print. 42 00:03:13,830 --> 00:03:15,680 This closure. 43 00:03:15,720 --> 00:03:25,830 So I'm going to get the closure of say hi closure for a value such as my name because obviously it receives 44 00:03:25,980 --> 00:03:27,640 one argument. 45 00:03:27,780 --> 00:03:36,990 So if Iran does not it should say hi and the name that comes off that in this case we want the space. 46 00:03:37,400 --> 00:03:39,670 Let's see if that is the case. 47 00:03:41,570 --> 00:03:45,160 Ari So so far I've managed to have a closure. 48 00:03:45,170 --> 00:03:52,150 That's the most basic closure that takes in an argument and does something and be understood kind of 49 00:03:52,150 --> 00:03:59,560 the format of it is whenever you want to have a closure you have this format curly brackets humans returns 50 00:03:59,650 --> 00:04:00,550 in. 51 00:04:00,550 --> 00:04:02,860 Now let's go ahead and write another one. 52 00:04:03,300 --> 00:04:07,560 So in this new one I'm willing to see some of the actual power of closures. 53 00:04:07,630 --> 00:04:13,390 I'm going to go ahead and actually I'm going to comment everything out in here so that you know they 54 00:04:13,390 --> 00:04:17,220 don't they don't get mixed down there and I'm printing values. 55 00:04:17,440 --> 00:04:20,880 And here's a little bit of an explanation of what I'm trying to do. 56 00:04:21,070 --> 00:04:24,310 Let's say I have a function on my function. 57 00:04:24,310 --> 00:04:31,630 You don't need to do this because I'm going to change real soon or my function is set my function and 58 00:04:31,720 --> 00:04:35,310 it has an argument as an as an entry to it. 59 00:04:35,320 --> 00:04:47,250 Let's say that argument is numbers all right all values song going to save values right and those values 60 00:04:47,250 --> 00:04:57,840 are an array of integers and then I'm going to do something on these array of values of whatever that 61 00:04:57,840 --> 00:05:06,240 thing is maybe I want to find out which of those are larger than a hundred so larger than 100. 62 00:05:06,500 --> 00:05:12,230 And then I want to have another function that goes ahead and says Well tell me the ones that are on 63 00:05:12,300 --> 00:05:19,350 this farm what I call fun the ones that are smaller than 100 and you can imagine this thing can continue 64 00:05:19,350 --> 00:05:25,860 for ever so many different functions each of them has a different let's call it condition. 65 00:05:25,860 --> 00:05:30,030 So here's the thing I have so many functions that are very similar. 66 00:05:30,030 --> 00:05:32,790 But each time the condition changes. 67 00:05:32,790 --> 00:05:39,450 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 68 00:05:39,460 --> 00:05:46,500 only have one function that can do so many different things and we can do that using Crozier's. 69 00:05:46,650 --> 00:05:54,510 Let's see how I'm going to manage that when I say give me a function Let's call this one multi multi 70 00:05:54,510 --> 00:06:01,730 purpose function multi per function like a cat on my multi-port function has these things. 71 00:06:01,830 --> 00:06:06,270 First of all it has a set of values that receive. 72 00:06:06,270 --> 00:06:08,310 So let's call it the vowels. 73 00:06:08,400 --> 00:06:12,570 And the vase is going to be an array of integers. 74 00:06:12,690 --> 00:06:16,470 So that's the integer values that we want to do something on them. 75 00:06:16,680 --> 00:06:20,160 But what we want to do and then I'm going to make it a closure. 76 00:06:20,190 --> 00:06:22,130 I'm going to say it's a closure. 77 00:06:22,260 --> 00:06:27,820 And that's just the name of a of a closure name of an argument you can call it whatever you like make 78 00:06:27,920 --> 00:06:29,690 sense to call that closure here. 79 00:06:29,730 --> 00:06:37,760 I'm going to say it's a closure that receives is an integer and returns a boolean. 80 00:06:37,770 --> 00:06:39,330 So let's see what that means. 81 00:06:39,330 --> 00:06:47,940 It means you will have a set of values and you will have another function as an entry for this function. 82 00:06:47,970 --> 00:06:53,910 I'm going to rewrite this once you are done as if it were two separate functions and you would see how 83 00:06:53,910 --> 00:06:54,660 it works. 84 00:06:54,690 --> 00:06:58,640 But for now I'm going to go ahead and say well here's what I want to do. 85 00:06:58,860 --> 00:07:04,400 I want to say for each number in vowels. 86 00:07:06,730 --> 00:07:08,340 This is what you want to do. 87 00:07:08,410 --> 00:07:16,070 You want to say effect closure of that number of each number. 88 00:07:16,060 --> 00:07:23,610 If that was a valid value I don't know just printed form to say print each name. 89 00:07:23,800 --> 00:07:26,140 That's all you're going to do for me right now. 90 00:07:26,310 --> 00:07:31,500 I could change the print to interpret it properly like. 91 00:07:31,780 --> 00:07:33,330 So what is the condition. 92 00:07:33,340 --> 00:07:34,390 What is it going to do. 93 00:07:34,390 --> 00:07:40,540 I know that the numbers might be I don't know 1 2 5 and 5 million 6 million whatever the numbers but 94 00:07:40,540 --> 00:07:45,610 what is the condition that is going to say if that was true do it. 95 00:07:45,610 --> 00:07:50,350 That's something that we can do on the fly as he called the function. 96 00:07:50,350 --> 00:07:51,410 Just go ahead and do it. 97 00:07:51,430 --> 00:08:01,390 And when I say multifunction moral purpose function which is here that numbers I'm going to give with 98 00:08:01,390 --> 00:08:04,850 an array of let's say 100 200. 99 00:08:04,870 --> 00:08:14,130 Seven hundred to twenty five and then the closure is going to say integer in some sort of condition 100 00:08:14,140 --> 00:08:22,840 and I'm going to say call this integer value and then say the condition is this As long as Val is below 101 00:08:23,140 --> 00:08:24,480 300. 102 00:08:24,970 --> 00:08:30,400 So as you can see I called a function with an argument. 103 00:08:30,400 --> 00:08:37,240 Another argument that that other argument of mine is actually a condition itself. 104 00:08:37,300 --> 00:08:41,780 So let's run this and see what we get if you Ronit below 300. 105 00:08:41,800 --> 00:08:48,040 We should get a few of these numbers 100 or 200 225 but maybe one or two. 106 00:08:48,100 --> 00:08:54,880 So either with a different condition I wanted to see a valid larger or equal to 200. 107 00:08:55,120 --> 00:08:56,140 Let's see what we get now. 108 00:08:56,240 --> 00:08:59,360 We should get only the 700 number. 109 00:08:59,570 --> 00:09:05,880 Now if I were to rewrite this whole thing using nested functions it would be something like this. 110 00:09:05,880 --> 00:09:10,590 And I really suggest you don't try this with me just just have a look at it because you're not going 111 00:09:10,590 --> 00:09:11,730 to really use it. 112 00:09:11,790 --> 00:09:20,520 It would be called a nested function a minus that would receive these values as our input arguments 113 00:09:21,010 --> 00:09:28,890 and then it would have a body which inside its body it would have another function called our internal 114 00:09:28,890 --> 00:09:36,420 function that my internal function receives of one value just like here it would receive one integer 115 00:09:36,420 --> 00:09:41,390 value and would give me back a boolean. 116 00:09:42,000 --> 00:09:44,320 And then what does it that that one do. 117 00:09:44,370 --> 00:09:56,490 One says for instance if one is in this case a smaller larger than 100 if it is larger than three hundred 118 00:09:56,670 --> 00:09:59,510 then return true. 119 00:10:00,080 --> 00:10:00,590 Right. 120 00:10:00,840 --> 00:10:04,150 So I may write this a little bit. 121 00:10:04,320 --> 00:10:09,060 I don't really like typing code like this but sometimes because I'm trying to zoom into the screen for 122 00:10:09,060 --> 00:10:10,980 you guys I have two. 123 00:10:11,190 --> 00:10:20,630 So one is if that is that and otherwise else and in the else I'm going to say here it's false. 124 00:10:20,670 --> 00:10:27,690 So there's an internal function that receives a number it is larger than something it returns true otherwise 125 00:10:27,690 --> 00:10:28,960 it is false. 126 00:10:29,040 --> 00:10:36,730 Now within the actual body of function it would say for each value you mean vows. 127 00:10:36,930 --> 00:10:38,940 Do this what should you do. 128 00:10:38,940 --> 00:10:50,580 You should say if the internal function of each vial was true then do what we did before. 129 00:10:50,580 --> 00:10:58,350 Preens this not so in our purines each. 130 00:10:59,040 --> 00:11:04,830 So this would be the longer we're on if I can show you both of them at the same time. 131 00:11:05,110 --> 00:11:12,060 So yes this would be a longer way around a nested set of functions and this would be an easier way around 132 00:11:12,100 --> 00:11:15,950 using a closure that a closure on the fly. 133 00:11:15,950 --> 00:11:20,660 Whenever I'm calling it I can actually decide what do you want to see. 134 00:11:20,790 --> 00:11:28,570 Also here I have only one condition but here I could actually decide about the condition as I go through. 135 00:11:28,570 --> 00:11:35,220 Like I could have this function and I would have another one say multi-purpose function the values or 136 00:11:35,220 --> 00:11:38,600 another set of values and that other set of values. 137 00:11:38,790 --> 00:11:48,030 The closure for it says for any value if Val is smaller than 500 so the same function is being used 138 00:11:48,120 --> 00:11:54,270 at the same time with no changes to the body of the function because in here with no changes to its 139 00:11:54,270 --> 00:12:02,170 body but is doing two different things for us which obviously is a lot simpler than writing nested functions. 140 00:12:02,350 --> 00:12:06,270 Some remove that nested function and well remove that. 141 00:12:06,270 --> 00:12:12,150 And now let's move on to another thing about closures which is called Escaping closures. 142 00:12:12,210 --> 00:12:20,310 So let's see what is an escaping closure escaping closure escaping closures or a particular type of 143 00:12:20,310 --> 00:12:28,410 closures that they get called within the arguments of a function and then they can return back from 144 00:12:28,410 --> 00:12:29,280 the function. 145 00:12:29,280 --> 00:12:37,590 They are primarily used for synchronous processes asynchronous process is essentially a process where 146 00:12:37,860 --> 00:12:44,880 you're reading something off the network and it has two word power of your are there threats out there. 147 00:12:44,910 --> 00:12:50,760 You say user interface thread or computation thread simply because when you make a connection with the 148 00:12:50,760 --> 00:12:55,670 network backend it might take a little while before you get some results. 149 00:12:55,680 --> 00:13:01,230 Let's say it takes half a second or one seconds or even you know if you're on a slow connection it might 150 00:13:01,230 --> 00:13:02,460 take two seconds. 151 00:13:02,460 --> 00:13:05,190 So in that time your app should unfreeze. 152 00:13:05,220 --> 00:13:07,200 So they have to work synchronously. 153 00:13:07,200 --> 00:13:09,800 They have to work parler each other. 154 00:13:09,930 --> 00:13:16,860 And that's you know what we do for situations like that we use a closure that waits for something and 155 00:13:16,860 --> 00:13:20,130 then gets essentially popped back. 156 00:13:20,130 --> 00:13:26,320 So with that in mind let's see how we're going to write an escaping closure and it going here and get 157 00:13:26,320 --> 00:13:26,920 us. 158 00:13:26,970 --> 00:13:31,550 We have a function that is going to read the photos from our backend. 159 00:13:31,620 --> 00:13:35,630 So I'm going to say function the search for talks. 160 00:13:35,670 --> 00:13:40,500 So this is a function that's going to fetch pictures for us from some sort of a back end that we will 161 00:13:40,540 --> 00:13:44,260 see in quite a few sections from now to be honest. 162 00:13:44,280 --> 00:13:47,760 But we can see the process of a closure in here. 163 00:13:48,060 --> 00:13:55,710 So I'm going to say my fecche photos has a link that is going to be a string that will read and refine 164 00:13:55,770 --> 00:13:58,860 it and then we want to have a closure for it. 165 00:13:59,430 --> 00:14:06,110 My closure I'm going to call it completion which is a very common naming convention people code completion. 166 00:14:06,110 --> 00:14:09,660 Troy prope tells them they completed or not. 167 00:14:09,900 --> 00:14:19,350 And I'm going to say my completion has these things has a boolean and has a set A and array of a string 168 00:14:19,740 --> 00:14:24,340 of the names of the photos that it's found. 169 00:14:24,390 --> 00:14:26,860 It doesn't return anything essentially. 170 00:14:27,030 --> 00:14:36,060 So I have a completion closure that returns whether it succeeds or not based on that returns a set of 171 00:14:36,060 --> 00:14:37,350 photos or not. 172 00:14:37,350 --> 00:14:42,900 And this may have made it optional because it's possible that this bullion is false and we have actually 173 00:14:42,900 --> 00:14:48,290 failed in fetching information and as a result these things are nil. 174 00:14:48,290 --> 00:14:49,810 There is nothing in them. 175 00:14:50,040 --> 00:14:58,360 And then I'm going to go ahead and say well this completion is actually escaping us. 176 00:14:58,420 --> 00:14:59,620 K ping. 177 00:14:59,940 --> 00:15:05,340 So it's an escaping closure which will be called later on. 178 00:15:05,340 --> 00:15:10,810 So similar to the other closures that we had a closure inside the body of the function. 179 00:15:10,820 --> 00:15:16,610 It gets called immediately but it doesn't get returned immediately on live here. 180 00:15:16,630 --> 00:15:23,060 That whatever is happening in the van and the bully and or whatever this statement is it must be returned 181 00:15:23,190 --> 00:15:30,460 right then and escaping closure essentially wait for it to be completed and then it gets returned. 182 00:15:30,600 --> 00:15:32,650 And that's essentially the power of it. 183 00:15:32,760 --> 00:15:34,680 So let's see how are we going to use that. 184 00:15:34,680 --> 00:15:39,120 So here I'm going to go ahead and say let's say we have a status. 185 00:15:39,120 --> 00:15:44,880 This is not this is far from us from the network status coming from the network. 186 00:15:45,000 --> 00:15:50,040 So now that we don't have access to a real network I'm going to essentially fake that value. 187 00:15:50,040 --> 00:15:56,640 I'm going to say let her status and I want to make her status to be something that is either true or 188 00:15:56,640 --> 00:15:57,600 it is false. 189 00:15:57,600 --> 00:16:05,480 So I'm going to say Give me an arc for random human or random off to reach this number is either one 190 00:16:05,490 --> 00:16:11,750 or zero simply because a random number below two is either 0 or 1. 191 00:16:11,850 --> 00:16:17,640 And I'm going to say if it is status it means if this was true. 192 00:16:17,790 --> 00:16:18,570 Right. 193 00:16:18,630 --> 00:16:26,130 If it's not those who want to do something I shouldn't have written it like that and else we should 194 00:16:26,130 --> 00:16:27,290 do something else. 195 00:16:27,300 --> 00:16:29,480 So let's see what we have to do. 196 00:16:30,020 --> 00:16:32,900 Let me get myself better spaces in here. 197 00:16:33,360 --> 00:16:35,740 So in the case of else that's quite clear. 198 00:16:35,760 --> 00:16:38,240 We have to say completion. 199 00:16:38,240 --> 00:16:43,290 We have to say completion false and we didn't get any images. 200 00:16:43,320 --> 00:16:45,690 That's exactly what he's expecting us to tell him. 201 00:16:45,810 --> 00:16:51,340 A boolean value and an array of strings which is optional. 202 00:16:51,870 --> 00:16:55,950 And then in case it was true I'm going to say completion. 203 00:16:56,190 --> 00:16:56,940 True. 204 00:16:57,180 --> 00:16:58,630 Yes it succeeded. 205 00:16:58,730 --> 00:17:05,920 And give us some we know of fake photos for it or one photo or two. 206 00:17:06,540 --> 00:17:08,280 So that's what we have in here. 207 00:17:08,280 --> 00:17:13,600 Let me read for my things a little bit again because I need to see everything once again. 208 00:17:13,620 --> 00:17:19,830 So now we have both of these statuses happening and we have our function. 209 00:17:19,980 --> 00:17:21,970 Let's go ahead and actually use it. 210 00:17:21,990 --> 00:17:25,410 I'm going to go ahead and say fecche photos. 211 00:17:25,410 --> 00:17:26,810 I said give me a link. 212 00:17:26,830 --> 00:17:29,040 So I'm going to say fake. 213 00:17:29,100 --> 00:17:35,060 Imagine that was an actual link and then it says what you want to get at the completion of the completion 214 00:17:35,070 --> 00:17:40,640 I want to get the result I want to get our photos Araik. 215 00:17:40,680 --> 00:17:42,730 Let's call it now. 216 00:17:42,750 --> 00:17:52,380 Here is what we do we say if the Rez if resulta was true it means after this event happened that that 217 00:17:52,380 --> 00:17:55,780 is the very long event that we currently don't have access to. 218 00:17:55,950 --> 00:18:02,820 But if that one happened and it was successful we got back all of these and if we had a successful result 219 00:18:03,270 --> 00:18:11,720 then use the photos Araik otherwise or tell us that oh there was an error. 220 00:18:11,760 --> 00:18:12,740 So on accident. 221 00:18:12,760 --> 00:18:13,130 Right. 222 00:18:13,140 --> 00:18:27,170 The other advisors say us do this Corinne's prints error in reading metaphor or something to that effect. 223 00:18:27,180 --> 00:18:33,570 Now obviously right now I'm faking Duvalier's I can't really be sure what happens but you could try 224 00:18:33,570 --> 00:18:33,670 it. 225 00:18:33,670 --> 00:18:39,180 Let me let me go ahead and comment these guys out as well. 226 00:18:39,960 --> 00:18:44,970 So I only have to deal with this one escaping closures for a Synchronoss tasks. 227 00:18:45,000 --> 00:18:50,850 So if you run this now depending on whether that number becomes true or false you either get to see 228 00:18:50,850 --> 00:18:57,660 the photos or we get like here error in reading networth because something happened so we couldn't get 229 00:18:57,660 --> 00:19:07,760 to the results by so that's about that closures escaping closures closures we use them as a way of passing 230 00:19:08,300 --> 00:19:16,760 functions as an argument into another function to avoid having nested functions something I personally 231 00:19:16,760 --> 00:19:18,410 do when you don't really have to do it. 232 00:19:18,410 --> 00:19:24,980 What they really really like it is I don't put my closures within the body of the argument or actually 233 00:19:24,980 --> 00:19:26,880 write them as a separate thing. 234 00:19:27,070 --> 00:19:33,260 So I essentially say type alias under Type early is that they want to call it. 235 00:19:33,560 --> 00:19:38,370 In this case on line closures or anything that you want to call it. 236 00:19:38,710 --> 00:19:43,070 And that becomes the entire Declaration of my closure. 237 00:19:43,280 --> 00:19:47,130 So it says on line closure is that so on and so forth. 238 00:19:47,270 --> 00:19:54,050 And then in my function I have a link of the type of string and I have a compilation of that type on 239 00:19:54,050 --> 00:20:00,290 line closure which is also escaping in the form of that little bit so it's easier to see. 240 00:20:00,290 --> 00:20:06,160 So essentially you managed to put my on line closure inside a separate variable. 241 00:20:06,290 --> 00:20:09,330 Like I said you didn't see this as a very common practice. 242 00:20:09,350 --> 00:20:15,650 I really like it this way simply because I end up using the same closure in so many different places 243 00:20:15,710 --> 00:20:23,360 I might be dealing with it uploading either no photos downloading photos or a lot of times your closure 244 00:20:23,360 --> 00:20:27,610 is just a bully and that says video or something completed or not. 245 00:20:27,620 --> 00:20:31,050 So I know that I'm using that to one closure everywhere. 246 00:20:31,160 --> 00:20:36,620 And if I want to modify they only modify it once so that's about that. 247 00:20:36,680 --> 00:20:44,650 We looked at different types of closures are simple closures just how they correlate to a function. 248 00:20:44,810 --> 00:20:51,950 We looked at how to write a function that receives multiple different you know conditions as a closure 249 00:20:52,010 --> 00:20:53,230 as one of its arguments. 250 00:20:53,240 --> 00:20:55,810 And each time it can give us a different result. 251 00:20:55,970 --> 00:21:00,890 And finally we look now at escaping closures and that's about that. 252 00:21:00,890 --> 00:21:03,130 With that in mind let's move on to our next lesson.