0 1 00:00:01,180 --> 00:00:01,610 All right. 1 2 00:00:01,640 --> 00:00:07,610 So previously, we've seen how the UITextFieldDelegate is implemented. 2 3 00:00:07,610 --> 00:00:12,410 We've also talked through how protocols work and the delegation pattern. 3 4 00:00:12,800 --> 00:00:19,940 But because this is quite a complex topic and it's quite abstract to wrap your head around, it's probably 4 5 00:00:19,940 --> 00:00:22,600 a good idea to watch these videos 5 6 00:00:22,640 --> 00:00:27,380 couple of times before it'll actually start to sink in. 6 7 00:00:27,380 --> 00:00:34,310 In this lesson, I want to show you the full picture. So we're going to create our own protocol, our own 7 8 00:00:34,310 --> 00:00:40,900 delegates, and we're going to be able to see the entire process without any of Apple's code. 8 9 00:00:40,940 --> 00:00:47,110 And I want to make it easy to understand through the use of an analogy. In this analogy, 9 10 00:00:47,120 --> 00:00:57,650 we have a emergency call center staff and his responsibility is to respond to emergency calls and dispatch 10 11 00:00:57,710 --> 00:01:00,950 emergency staff as and when needed. 11 12 00:01:00,950 --> 00:01:12,860 So this is our class of emergency call handler. And the emergency call handler has access to a bleep. 12 13 00:01:12,950 --> 00:01:17,320 So I've never seen a bleep either until I started working in a hospital, 13 14 00:01:17,510 --> 00:01:24,350 and it's this very old and strangely very expensive piece of technology which is kind of like an 14 15 00:01:24,350 --> 00:01:32,840 old-school mobile phone where you can call the bleep, it has a phone number, and you can trigger it to report 15 16 00:01:32,840 --> 00:01:35,930 a message on the screen, as well as bleep, 16 17 00:01:35,960 --> 00:01:37,000 so make a sound, 17 18 00:01:37,160 --> 00:01:41,600 and sometimes you can even get it to play recorded messages. 18 19 00:01:41,880 --> 00:01:50,330 And in this case, our emergency call handler can activate this bleep remotely, and get it to trigger and 19 20 00:01:50,390 --> 00:01:57,320 play a sound, and notify the person that carries the bleep that there is a cardiac arrest, somebody's 20 21 00:01:57,320 --> 00:02:01,370 heart has stopped, and you need to go and perform CPR. 21 22 00:02:01,370 --> 00:02:09,260 So in this case, the bleep is going to be the delegate property, and whoever carries the bleep or whoever 22 23 00:02:09,260 --> 00:02:15,380 is the delegate is the one that's going to be notified when they need to perform some action. 23 24 00:02:15,560 --> 00:02:23,360 Now, the emergency call handler doesn't actually care who carries this bleep or who is the delegate. 24 25 00:02:23,360 --> 00:02:30,860 All it cares is that in order to be able to carry the cardiac arrest bleep, you must have first completed 25 26 00:02:31,100 --> 00:02:33,100 the Advanced Life Support Course. 26 27 00:02:33,110 --> 00:02:41,240 So this is a two-day course where you learn how to resuscitate somebody or bring them back to life through 27 28 00:02:41,240 --> 00:02:42,760 the use of various methods. 28 29 00:02:42,770 --> 00:02:50,580 And one of the methods you learn is CPR. The emergency call handler knows that whoever is the delegate, 29 30 00:02:50,620 --> 00:02:54,300 whoever carries the bleep must have completed this course, 30 31 00:02:54,440 --> 00:02:58,130 and so they must adopt this protocol. 31 32 00:02:58,490 --> 00:03:05,840 So this is the AdvancedLifeSupport protocol. And whoever has adopted this AdvancedLifeSupport protocol 32 33 00:03:06,290 --> 00:03:12,370 will, of course, surely know how to perform the CPR method. 33 34 00:03:12,630 --> 00:03:19,590 So now that everything is set up, a day goes by and a new day begins. 34 35 00:03:19,620 --> 00:03:27,900 So on this beautiful morning, Pete, the paramedic, comes in and he is the one who is on shift today to 35 36 00:03:27,900 --> 00:03:29,520 carry the bleep. 36 37 00:03:29,610 --> 00:03:35,490 So he is carrying around this bleep which can go off at any minute notifying him where he needs to be 37 38 00:03:35,520 --> 00:03:37,890 to perform CPR. 38 39 00:03:38,070 --> 00:03:46,200 And so the emergency call handler at some unknown time will get a call and he will trigger the bleep 39 40 00:03:46,410 --> 00:03:47,700 to go off. 40 41 00:03:47,700 --> 00:03:52,830 Often when you're working as a doctor, the bleep is the bane of your life. 41 42 00:03:53,370 --> 00:03:59,700 I love the classic thing that the doctor does. They're always checking the time and saying, "Oh, God. What am I working?" 42 43 00:03:59,700 --> 00:04:05,910 Now, that the emergency call handler has triggered the bleep, 43 44 00:04:05,910 --> 00:04:11,880 it basically tells whoever is the delegate or whoever is carrying the bleep that you must go here and 44 45 00:04:11,880 --> 00:04:13,710 perform CPR. 45 46 00:04:13,710 --> 00:04:17,560 Now, because today is the paramedic Pete who's on call, 46 47 00:04:17,700 --> 00:04:23,460 then he's the one who gets the message and he goes and does that action. 47 48 00:04:23,460 --> 00:04:30,330 And we know that he can do that action because he has the AdvancedLifeSupport certificate or protocol, 48 49 00:04:31,110 --> 00:04:37,290 so the emergency call handler can be safe in the knowledge that when they trigger this pager or this 49 50 00:04:37,320 --> 00:04:44,520 bleep, that whoever is carrying it, is able to perform CPR. And they're able to notify the delegates or 50 51 00:04:44,550 --> 00:04:52,410 the person on call to perform that action by calling delegate.performCPR. 51 52 00:04:52,410 --> 00:04:58,860 Now, the beauty of this is, of course, that it might be the Paramedic Pete who's on call, it might be the 52 53 00:04:58,890 --> 00:04:59,510 Dr. Dave 53 54 00:04:59,610 --> 00:05:07,410 who's on call, or it could be the Surgeon Sally who is carrying the pager or the bleep. But to the 54 55 00:05:07,410 --> 00:05:09,120 emergency call handler, 55 56 00:05:09,120 --> 00:05:16,410 they don't need to care. All that they care about is that as long as there is somebody carrying the pager 56 57 00:05:16,410 --> 00:05:23,460 or the bleep, they can trigger the method perform CPR and whoever it is, whichever class it is that is 57 58 00:05:23,730 --> 00:05:28,080 set as the delegate will be notified and will perform that functionality. 58 59 00:05:29,360 --> 00:05:37,800 So let's convert all of this logic into code. Inside Xcode, I'm going to create another new Project. 59 60 00:05:39,550 --> 00:05:42,990 And it's, again, going to be a macOS command line tool. 60 61 00:05:43,330 --> 00:05:51,800 And I'm going to call it Protocols and Delegates. So we're gonna head over to our main.swift and 61 62 00:05:51,860 --> 00:05:54,870 delete everything as usual. 62 63 00:05:54,860 --> 00:05:59,390 And the first thing I'm gonna do is I'm going to create that protocol. 63 64 00:05:59,390 --> 00:06:02,010 So I'm going to create that certificate effectively. 64 65 00:06:02,110 --> 00:06:07,040 I use the protocol keyword and I give my protocol a name which, in this case, is going to be called the 65 66 00:06:07,490 --> 00:06:10,670 AdvancedLifeSupport. 66 67 00:06:10,670 --> 00:06:18,530 Now, this AdvancedLifeSupport protocol has just one requirement. In order to adopt this protocol, in 67 68 00:06:18,530 --> 00:06:21,190 order to get this AdvancedLifeSupport certificate, 68 69 00:06:21,200 --> 00:06:30,380 you must be able to know how to performCPR. And there's no implementation in the protocol, 69 70 00:06:30,380 --> 00:06:37,370 it's up to the person who is adopting this protocol to say how they're going to perform CPR, but they 70 71 00:06:37,370 --> 00:06:39,200 must know how to do this, 71 72 00:06:39,290 --> 00:06:44,930 so they must define this function somewhere within their class or their struct. 72 73 00:06:44,930 --> 00:06:54,110 Now comes along our emergency call handler. And this class is going to have a important property which 73 74 00:06:54,110 --> 00:07:00,380 is the delegate property, and the delegate property has a data type. 74 75 00:07:00,440 --> 00:07:06,980 It has a advanced life support protocol as the data type. 75 76 00:07:07,040 --> 00:07:15,620 So this means that whoever sets themselves as the delegate, any class or struct must have the advanced 76 77 00:07:15,620 --> 00:07:21,530 life support protocol adopted, so they must be able to know how to performCPR. 77 78 00:07:22,220 --> 00:07:27,230 So the emergency call handler has a couple of functionalities or methods. 78 79 00:07:27,410 --> 00:07:29,690 For example, they might be able to take calls, 79 80 00:07:29,690 --> 00:07:32,700 they might be able to assess the situation, 80 81 00:07:33,050 --> 00:07:39,620 where are you at, and what happens. To keep this simple, let's just say that assessSituation involves 81 82 00:07:39,770 --> 00:07:42,990 asking the caller, "Can you tell me what happened?" 82 83 00:07:43,130 --> 00:07:49,360 But they also have the functionality of triggering a medicalEmergency. 83 84 00:07:49,580 --> 00:07:56,780 So in this case, what they're going to do is they're going to call upon the delegate, whoever it may be, 84 85 00:07:56,900 --> 00:08:04,310 and they have no knowledge or care of who the delegate is, but all they need to do is say, "Whoever is 85 86 00:08:04,310 --> 00:08:11,940 on call, whoever is carrying the pager, please perform CPR on this patient." 86 87 00:08:12,020 --> 00:08:20,300 And so now let's define who the delegate may be. Let's define our paramedic. Paramedic. And this paramedic 87 88 00:08:20,390 --> 00:08:24,350 is going to adopt the AdvancedLifeSupport protocol. 88 89 00:08:24,740 --> 00:08:27,960 So they've been on the course, they've got the certificate, 89 90 00:08:28,160 --> 00:08:34,270 and what this means is they must be able to know how to perform CPR. 90 91 00:08:34,460 --> 00:08:39,140 And you can see that this is the error that we're getting from Xcode at the moment: 91 92 00:08:39,140 --> 00:08:48,020 Type 'Paramedic' does not conform to protocol 'Advanced Life Support" because in order to conform, you must have 92 93 00:08:48,140 --> 00:08:52,110 some implementation of this method could performCPR. 93 94 00:08:52,130 --> 00:09:00,530 So if we go ahead and click fix, it'll add that function into our Paramedic struct, and we now have to 94 95 00:09:00,770 --> 00:09:09,330 define what happens when this method gets triggered. In our case, "The paramedic is simply going to do 95 96 00:09:09,330 --> 00:09:15,840 some chest compressions, 30 per second." And they know how to do this because they went on the course, 96 97 00:09:15,840 --> 00:09:21,190 and so they've got the certificate, and they know how to implement performCPR. 97 98 00:09:21,400 --> 00:09:30,160 And in addition, when the paramedic goes on shift when they are initialized, they get told who the handler 98 99 00:09:30,250 --> 00:09:30,810 is. 99 100 00:09:30,850 --> 00:09:35,540 So the handler is, of course, of class EmergencyCallHandler. 100 101 00:09:35,890 --> 00:09:42,220 So when the paramedic goes on shift, the first thing they do is they pick up the bleep or the pager and 101 102 00:09:42,220 --> 00:09:47,440 they set the handler's delegate property as themselves. 102 103 00:09:47,440 --> 00:09:55,630 This is the equivalent of picking up that pager, which is the emergency pager, and they're now on call. 103 104 00:09:55,630 --> 00:10:02,620 And through this line of code, they've said, "I am going to listen for notifications when I have to perform 104 105 00:10:02,620 --> 00:10:05,920 CPR from the emergency call handler. 105 106 00:10:06,100 --> 00:10:12,930 I know who the handler is. And when they tell me to perform CPR that's exactly what I'm gonna do." 106 107 00:10:13,180 --> 00:10:19,900 Now that we've defined all of our classes, structs, protocols, whatever it may be, it's time to create our objects 107 108 00:10:19,960 --> 00:10:22,180 from these blueprints. 108 109 00:10:22,180 --> 00:10:25,580 The first thing we've got is an EmergencyCallHandler, 109 110 00:10:25,600 --> 00:10:29,550 so we'll call him Emilio. Emilio, 110 111 00:10:29,650 --> 00:10:31,840 the EmergencyCallHandler. 111 112 00:10:32,200 --> 00:10:36,690 And then, we've also got Pete the paramedic who's on call. 112 113 00:10:37,060 --> 00:10:44,410 And when the paramedic gets started with his day, he knows who the handler is. 113 114 00:10:44,440 --> 00:10:51,310 He gets notified that today you're on call and Emilio is going to be the one who's going to give you 114 115 00:10:51,310 --> 00:10:52,450 notifications, 115 116 00:10:52,450 --> 00:10:56,990 tell you when you need to go and perform CPR. 116 117 00:10:57,070 --> 00:11:01,380 Now we've got Emilio and Pete, both working and on their shifts. 117 118 00:11:01,510 --> 00:11:08,770 So at some point, Emilio gets a call and he's going to, first, assess the situation, what's happening, what's 118 119 00:11:08,770 --> 00:11:10,780 going on, where are they based, 119 120 00:11:10,810 --> 00:11:13,940 and then he deems that this is an emergency. 120 121 00:11:13,960 --> 00:11:18,270 So he is going to trigger a medical emergency. 121 122 00:11:18,340 --> 00:11:19,970 And when that happens, 122 123 00:11:19,990 --> 00:11:22,170 so let's run our code, 123 124 00:11:22,360 --> 00:11:27,340 the first thing that gets printed is Emilio is assessing the situation, so he's asking the caller can 124 125 00:11:27,340 --> 00:11:28,670 you tell me what happened. 125 126 00:11:28,720 --> 00:11:35,590 The next thing that happens is the medical emergency and this triggers the delegate whoever is set as 126 127 00:11:35,590 --> 00:11:37,660 the delegate to perform CPR. 127 128 00:11:37,870 --> 00:11:44,690 And in this case, it is, of course, our paramedic who's on call because he's set himself as the delegate. 128 129 00:11:44,710 --> 00:11:51,600 So when Emilio triggers the medical emergency, the bleep goes off, and it's the paramedic who's on call, 129 130 00:11:51,610 --> 00:11:54,850 so he goes and does the CPR. 130 131 00:11:54,850 --> 00:12:04,360 Now, this is all possible because of the fact that we can use the protocol as a data type. So in this 131 132 00:12:04,360 --> 00:12:11,770 case, the EmergencyCallHandler has this delegate property which requires that whoever sets themselves 132 133 00:12:11,800 --> 00:12:20,470 as the delegate must have this data type. They must have the AdvancedLifeSupport protocol adopted. 133 134 00:12:20,470 --> 00:12:26,530 And the reason why they must have the protocol adopted is because the EmergencyCallHandler knows that. 134 135 00:12:26,650 --> 00:12:33,330 If you've adopted the AdvancedLifeSupport, then you must have this functionality of performing CPR. 135 136 00:12:33,760 --> 00:12:40,450 So when they need to call upon the delegate to performCPR, they don't actually care who is the delegate, 136 137 00:12:40,690 --> 00:12:43,990 what class it is, what Superclass it might have. 137 138 00:12:44,020 --> 00:12:49,210 All they care is that they must be of the type AdvancedLifeSupport. 138 139 00:12:49,240 --> 00:12:56,890 So this means that we could create some other classes. Let's say I've got a class, instead of struct here, 139 140 00:12:56,980 --> 00:12:59,110 and this is a Doctor class. 140 141 00:12:59,740 --> 00:13:06,130 And the Doctor class doesn't actually have a Superclass, it's just a class as it is. 141 142 00:13:06,510 --> 00:13:11,410 But the Doctor class has adopted the AdvancedLifeSupport protocols, 142 143 00:13:11,420 --> 00:13:13,550 so they've also been on the course. 143 144 00:13:13,830 --> 00:13:18,210 And that means that they know how to performCPR. 144 145 00:13:18,270 --> 00:13:22,120 So let's add that function performCPR in here. 145 146 00:13:22,290 --> 00:13:28,740 Now, when they performCPR, all that's going to happen is they're going to follow the AdvancedLifeSupport 146 147 00:13:28,740 --> 00:13:33,540 protocol, and they're just going to do some chest compressions, 30 per second. 147 148 00:13:33,540 --> 00:13:40,800 Now, the doctor also has some other functionality, and maybe they could--maybe they also like using their 148 149 00:13:40,800 --> 00:13:50,490 stethoscopes. And when they do that, all that we do is, we'll say, they print "Listening for heart sounds." 149 150 00:13:50,490 --> 00:13:53,220 And the doctor similar to the paramedic, 150 151 00:13:53,220 --> 00:13:59,610 the first thing that they do when they get initialized or when they start their shift is they set themselves 151 152 00:13:59,940 --> 00:14:08,730 up with the EmergencyCallHandler, so they know that there is a EmergencyCallHandler and they set 152 153 00:14:08,820 --> 00:14:10,830 themselves as the delegate. 153 154 00:14:10,830 --> 00:14:16,670 They put on the pager and they are saying that when I go on shift, I'm gonna be the one that is gonna 154 155 00:14:16,680 --> 00:14:17,680 be the delegate. 155 156 00:14:17,820 --> 00:14:20,660 That's going to be notified when I need to performCPR. 156 157 00:14:20,850 --> 00:14:27,130 And if the EmergencyCallHandler triggers this method, then that's exactly what I'm going to do. 157 158 00:14:27,390 --> 00:14:35,920 Now, in addition, there could be a Surgeon class and the Surgeon class inherits from the Doctor class. 158 159 00:14:36,030 --> 00:14:42,900 So that means they immediately, by default, adopt the AdvancedLifeSupport because it's inherited from 159 160 00:14:42,900 --> 00:14:43,650 the Doctor class. 160 161 00:14:44,190 --> 00:14:52,620 And it also means that they already have access to the method performCPR. But they can, of course, override 161 162 00:14:52,620 --> 00:14:52,980 it. 162 163 00:14:52,980 --> 00:15:00,090 So they could say, "We'll do what the doctor would do when they need to perform CPR. But in addition, because 163 164 00:15:00,120 --> 00:15:07,050 we are surgeons, we also like to do a little bit of singing while we perform CPR. 164 165 00:15:07,050 --> 00:15:18,220 So we're going to sing 'Staying Alive' by the Bee Gees while we perform our chest compressions." 165 166 00:15:18,600 --> 00:15:25,260 Now in addition, maybe the surgeon also has some extra functionality, say, they are really good with electric 166 167 00:15:25,260 --> 00:15:28,990 drills which just make whirring sounds when you play with it, 167 168 00:15:29,010 --> 00:15:30,540 I mean, use it. 168 169 00:15:30,540 --> 00:15:31,200 So there we go. 169 170 00:15:31,200 --> 00:15:34,830 We've now defined a Doctor class, as well as a Surgeon class, 170 171 00:15:34,830 --> 00:15:42,360 and we're now able to put some other guys on shift. So, let's say that today it's not Pete who's on shift, 171 172 00:15:42,420 --> 00:15:46,470 let's say it's Angela the surgeon who's on shift. 172 173 00:15:46,680 --> 00:15:52,530 And the first thing I do, of course, is go ahead and set myself up with Emilio. 173 174 00:15:52,800 --> 00:16:00,150 And that means that I'm going to be the one carrying the pager and listening for when Emilio will trigger 174 175 00:16:00,450 --> 00:16:03,180 the performCPR method. 175 176 00:16:03,180 --> 00:16:10,050 And so now when Emilio triggers a medicalEmergency if we run the code, notice how we haven't changed 176 177 00:16:10,140 --> 00:16:14,060 any of the code that Emilio has performed, 177 178 00:16:14,280 --> 00:16:20,190 but as soon as the medicaEmergency goes off, it's actually the surgeon that does the chest compressions 178 179 00:16:20,280 --> 00:16:23,540 while singing "Staying Alive" by the Bee Gees. 179 180 00:16:23,940 --> 00:16:27,540 So it's, of course, this method that's getting called. 180 181 00:16:27,630 --> 00:16:36,030 So now we've seen how our Paramedic struct can adopt the AdvancedLifeSupport protocol, our Class doctor 181 182 00:16:36,060 --> 00:16:44,250 can adopt the AdvancedLifeSupport protocol, our class Surgeon which inherits from Doctor is automatically 182 183 00:16:44,280 --> 00:16:47,280 adopting the AdvancedLifeSupport protocol. 183 184 00:16:47,400 --> 00:16:57,270 And in this case, the EmergencyCallHandler, which is taking the role of our UITextField, can be completely, 184 185 00:16:57,630 --> 00:17:05,670 blissfully unaware of who is going to be performing the CPR, which class, or whatever they may be, as long 185 186 00:17:05,670 --> 00:17:11,730 as they adopt the AdvancedLifeSupport, we can trigger the performCPR method, and it could be the paramedic 186 187 00:17:11,760 --> 00:17:16,500 that's on call, it could be the doctor, or it could be the surgeon, and the honey badger, 187 188 00:17:16,890 --> 00:17:24,430 I mean, the EmergencyCallHandler doesn't care. So that's an overview of how we would create our own 188 189 00:17:24,520 --> 00:17:27,130 delegates and protocols. 189 190 00:17:27,130 --> 00:17:33,910 And so now that we're armed with this knowledge, we can be more dangerous. And in our project, we can start 190 191 00:17:33,910 --> 00:17:37,300 coding up the functionality for getting the weather data.