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