1
00:00:03,390 --> 00:00:12,440
Hey Jack is here with themselves to welcome you back for part two of our journey into protocol oriented

2
00:00:12,440 --> 00:00:13,280
programming.

3
00:00:14,560 --> 00:00:20,220
In the last lesson we talked about the difference between reference and value types or classes versus

4
00:00:20,220 --> 00:00:21,780
structures.

5
00:00:21,780 --> 00:00:29,700
One of the limitations that you tap taps have had in the past is that I do not offer inheritance with

6
00:00:29,700 --> 00:00:30,840
classes in swift.

7
00:00:30,840 --> 00:00:33,140
We have single inheritance.

8
00:00:33,300 --> 00:00:38,400
This means that we can implement functionality in a superclass and then use that functionality in our

9
00:00:38,400 --> 00:00:40,240
subclasses.

10
00:00:40,260 --> 00:00:46,470
Some languages offer multiple inherit inheritance where classes can have more than one superclass but

11
00:00:46,470 --> 00:00:53,960
in swift we're limited to exactly one superclass the inheritance problem with value types has virtually

12
00:00:53,960 --> 00:00:54,710
been eliminated.

13
00:00:54,710 --> 00:01:02,650
With the advent of protocols and protocol extensions protocols are kind of like contracts or blueprints.

14
00:01:02,870 --> 00:01:09,140
They define what functionality a class or structure are all for but they do not provide any implementation

15
00:01:09,140 --> 00:01:09,920
code.

16
00:01:09,980 --> 00:01:16,010
They simply define requirements for types that conform to that protocol.

17
00:01:16,010 --> 00:01:22,950
Some other languages offer a similar feature namely interfaces with protocol extensions you can now

18
00:01:22,970 --> 00:01:30,110
extend those protocols and any types that conform to them and actually provide common functionality.

19
00:01:30,110 --> 00:01:35,420
This functionality or implementation code can be functions or computed properties.

20
00:01:35,420 --> 00:01:40,790
Note here that properties must be computed values cannot be assigned directly to properties of protocol

21
00:01:40,790 --> 00:01:42,240
extensions.

22
00:01:42,470 --> 00:01:47,750
In this lesson we're going to go through a playground project and take a look at protocols and the next

23
00:01:47,750 --> 00:01:54,410
list will add and protocol extensions and finally we'll briefly cover generics before we move on to

24
00:01:54,410 --> 00:02:00,920
some real world examples of ways to incorporate protocol into programming into your everyday code.

25
00:02:01,770 --> 00:02:04,400
So I was going to pull up a new playground here.

26
00:02:05,410 --> 00:02:11,100
To get started and we'll just call this protocol's

27
00:02:13,620 --> 00:02:16,800
and we'll leave it is our way saying we'll save it on the desktop

28
00:02:19,630 --> 00:02:24,040
or get rid of that line there.

29
00:02:24,080 --> 00:02:34,020
Now the first thing we're going to do is we'll go ahead and create a protocol for a vehicle.

30
00:02:34,070 --> 00:02:40,220
So in this instance we could do if we were modeling using reference types or classes we could have a

31
00:02:40,220 --> 00:02:47,360
base class that was a vehicle and we could extend that to subclasses in the form of more specialized

32
00:02:47,870 --> 00:02:49,080
classes.

33
00:02:49,130 --> 00:02:53,020
In this case this is all about protocol so that's what we're going to do here.

34
00:02:53,450 --> 00:02:54,880
So.

35
00:02:55,310 --> 00:03:02,810
So define a protocol of vehicle and we're going to have a property here for it and we'll just call it

36
00:03:03,110 --> 00:03:12,980
is running and we'll make that a boolean and in protocol's you define whether it's Read-Only property

37
00:03:13,460 --> 00:03:15,070
or reroute property.

38
00:03:15,290 --> 00:03:23,210
In this particular protocol's going to be a read write property so we've got to specify get set there.

39
00:03:24,160 --> 00:03:26,970
All right.

40
00:03:27,800 --> 00:03:33,320
Now we're going to have a couple of functions in here and since we might be using this with a structure

41
00:03:33,980 --> 00:03:36,680
there's one little caveat here.

42
00:03:36,680 --> 00:03:43,100
When you define a function in a protocol so we're going to say thanks start.

43
00:03:43,130 --> 00:03:44,660
And this is just going to be a function.

44
00:03:44,670 --> 00:03:50,870
Again these are just blueprints or contracts and whatever conforms to it has to implement these functions

45
00:03:50,870 --> 00:03:52,960
in their body.

46
00:03:53,180 --> 00:04:01,530
So we're going to have a start function and we're also going to have a turn or function.

47
00:04:01,550 --> 00:04:06,410
The caveat here is if you use these with value types you have to specify

48
00:04:09,650 --> 00:04:12,300
mutating in front of the function.

49
00:04:12,470 --> 00:04:21,440
If it's going to mutate a property inside the structure or a value top that is going to conform to this

50
00:04:21,910 --> 00:04:24,380
so we'll go ahead and add that in here.

51
00:04:24,980 --> 00:04:30,260
All you need to know is if you're going to use structures with this and you're going to have functions

52
00:04:30,260 --> 00:04:35,460
that modify internal properties and you have to put that mutating keyword in there.

53
00:04:35,540 --> 00:04:37,300
So this is just Luxa.

54
00:04:37,310 --> 00:04:44,720
This is a real simple example but we've got our vehicle protocol here and everyone go ahead and create

55
00:04:44,720 --> 00:04:50,690
a sports car that is going to conform to the vehicle protocol and the sports car is going to be a struct

56
00:04:50,840 --> 00:04:56,210
so we'll use a value top here.

57
00:04:56,920 --> 00:04:57,600
All right.

58
00:04:57,730 --> 00:05:05,390
And then now we've got data since it conforms to vehicle you'll see here the error it says top sports

59
00:05:05,390 --> 00:05:07,940
car does not conform to a protocol vehicle.

60
00:05:08,300 --> 00:05:14,990
Well that's because we haven't implemented any of those required properties or method yet.

61
00:05:14,990 --> 00:05:17,000
So now we need to go in here in

62
00:05:20,590 --> 00:05:25,890
specify our properties so we're going to set this one the false by default.

63
00:05:26,640 --> 00:05:29,150
And now we've got to do our functions.

64
00:05:29,350 --> 00:05:37,910
So this being a structure we need the mutating keyword and you'll notice there that it can.

65
00:05:38,140 --> 00:05:41,790
Since we're conforming to that topic kind of feel some of that for you.

66
00:05:42,340 --> 00:05:48,450
So in the Star function we're just going to say it is running.

67
00:05:49,120 --> 00:05:51,640
So if it's already running we're just going to print it

68
00:05:54,730 --> 00:05:57,430
already started.

69
00:05:57,430 --> 00:06:04,210
Otherwise we're going to say is running equals true.

70
00:06:04,230 --> 00:06:05,590
In the end we're going to present

71
00:06:08,840 --> 00:06:10,580
room.

72
00:06:10,700 --> 00:06:12,980
Think that's kind of how sports car sales are.

73
00:06:13,240 --> 00:06:14,800
12 Hardeen.

74
00:06:15,450 --> 00:06:25,380
All right now we've got another function here that we have to implement and this one is all.

75
00:06:26,430 --> 00:06:36,560
So we're going to say it is running and we're going to say is running equals false and we're going to

76
00:06:36,570 --> 00:06:40,550
preempt crickets.

77
00:06:40,610 --> 00:06:41,530
I have no idea.

78
00:06:41,540 --> 00:06:43,880
It's just.

79
00:06:44,360 --> 00:06:48,100
Otherwise we're going to say prent.

80
00:06:48,220 --> 00:06:50,810
I already did.

81
00:06:52,360 --> 00:06:53,030
OK.

82
00:06:53,270 --> 00:06:56,710
So we've got our is running property here.

83
00:06:56,990 --> 00:07:00,340
We've got our start function in our turn off function.

84
00:07:00,460 --> 00:07:03,310
And you'll notice here that we've lost our error.

85
00:07:03,380 --> 00:07:10,190
So now we conform to the vehicle protocol now that we've implemented all of those properties and methods

86
00:07:11,780 --> 00:07:14,050
and protocols.

87
00:07:14,810 --> 00:07:20,870
Any any type can conform to a protocol so it doesn't have to be a value type it can be a reference type.

88
00:07:20,960 --> 00:07:29,330
So just to prove this we'll go ahead Mike our next one a class so we'll say a semi truck and we'll conform

89
00:07:29,330 --> 00:07:32,210
to the vehicle protocol.

90
00:07:32,900 --> 00:07:34,900
And again you'll see here we get our error.

91
00:07:34,910 --> 00:07:36,310
It does not conform.

92
00:07:36,650 --> 00:07:47,310
So now we need to add in our property and we'll say false to begin with we make a little bit more space

93
00:07:47,310 --> 00:07:47,830
here.

94
00:07:50,690 --> 00:07:50,950
All right.

95
00:07:50,960 --> 00:07:56,990
Now this is a class so the mutating keyword is not required here.

96
00:07:58,440 --> 00:08:03,150
Made a reference tot the properties in here are immutable.

97
00:08:03,240 --> 00:08:10,410
We don't need the mutating key word and it doesn't hurt that it's seeing that it's in the protocol declaration

98
00:08:10,770 --> 00:08:17,630
that is just in case you use it in a structure then you're able to modify your properties.

99
00:08:17,760 --> 00:08:30,120
So in the class we're just going to say phunk start and we're going to say if it is running roadside

100
00:08:30,130 --> 00:08:30,820
print

101
00:08:34,070 --> 00:08:37,620
already started.

102
00:08:37,620 --> 00:08:46,740
Otherwise we're going to say is running is true and we're going to present.

103
00:08:47,400 --> 00:08:50,700
I don't know how does an 18 wheeler sound rumble.

104
00:08:50,940 --> 00:08:53,460
Maybe that's a good word for it.

105
00:08:53,460 --> 00:08:53,830
All right.

106
00:08:53,840 --> 00:08:57,330
So we've got to turn our star functionality or turn off function

107
00:09:00,150 --> 00:09:08,340
so we'll say if it is running and we're going to kill it here so we'll say is running equals false hopes

108
00:09:09,900 --> 00:09:11,510
and we'll say Brant

109
00:09:15,160 --> 00:09:17,140
I don't know.

110
00:09:17,640 --> 00:09:19,560
Silence.

111
00:09:22,770 --> 00:09:27,740
And then if it's not running we'll say

112
00:09:30,580 --> 00:09:36,790
already shut down I guess.

113
00:09:36,860 --> 00:09:38,610
All right.

114
00:09:39,030 --> 00:09:42,660
So hopefully you need those if else is backwards.

115
00:09:42,700 --> 00:09:45,840
But OK now the end.

116
00:09:46,330 --> 00:09:53,890
You can also just because you conform to that vehicle protocol you can still add other functionality

117
00:09:53,890 --> 00:09:54,610
in here.

118
00:09:54,940 --> 00:09:59,650
So let's say in the case of the 18 wheeler we want to add another function

119
00:10:02,520 --> 00:10:07,330
called blow air horn.

120
00:10:07,330 --> 00:10:14,900
If I can type and we're just going to preach it how does an air horn sound.

121
00:10:18,950 --> 00:10:20,960
What is a tune.

122
00:10:20,970 --> 00:10:22,630
All right.

123
00:10:23,530 --> 00:10:29,760
So now we've got our semi truck class which conforms to vehicle and we've got our sports car struck

124
00:10:29,770 --> 00:10:32,980
that conforms to vehicle.

125
00:10:33,040 --> 00:10:34,350
So let's move on down.

126
00:10:34,360 --> 00:10:37,200
Let's go and create some instances here.

127
00:10:37,840 --> 00:10:53,120
So we'll say car one is equal to a sports car and say truck one is a call to a semi truck.

128
00:10:53,410 --> 00:10:53,890
All right.

129
00:10:53,900 --> 00:10:57,450
Now we can do car want to start.

130
00:10:58,070 --> 00:10:59,810
We can do truck one

131
00:11:02,950 --> 00:11:12,280
star and we can say truck one blow and we pull up our console down here.

132
00:11:12,290 --> 00:11:14,280
You can see that we're putting these things out.

133
00:11:16,190 --> 00:11:21,110
So we've successfully conformed to that protocol.

134
00:11:21,780 --> 00:11:27,880
And the real power with protocols coming in with protocol extensions are a lot of the power.

135
00:11:27,900 --> 00:11:29,930
And we'll go over that in the next lesson.

136
00:11:30,090 --> 00:11:40,360
But one really cool thing about this let's go ahead and turn these off.

137
00:11:40,900 --> 00:11:43,580
So we've got them both shut down.

138
00:11:43,660 --> 00:11:51,460
One really cool thing about this is we can do something like we can make a vehicle array and we'll just

139
00:11:52,030 --> 00:11:59,710
instead of using the brackets syntax we'll use this the bracket syntax is actually an alternative syntax.

140
00:11:59,860 --> 00:12:03,130
This right here is actually house defined in the standard library.

141
00:12:03,700 --> 00:12:13,090
And for the tab we're going to make it a vehicle top so we're going to put in here car one and truck

142
00:12:13,090 --> 00:12:16,820
one.

143
00:12:17,000 --> 00:12:22,490
So now we've got an array even though these are two different types.

144
00:12:22,500 --> 00:12:24,950
One is a sports car and one's of Sumatra.

145
00:12:25,160 --> 00:12:29,450
They're in the same right because they both conform to vehicle.

146
00:12:29,930 --> 00:12:33,210
So you can use protocols as tops.

147
00:12:33,300 --> 00:12:35,480
This in this situation.

148
00:12:35,660 --> 00:12:37,550
And then if we go ahead and walk through them

149
00:13:02,170 --> 00:13:04,760
we'll just print out the vehicle.

150
00:13:04,760 --> 00:13:10,820
So now you can see down here we've got a sports car and it's got the is running is false and we just

151
00:13:10,820 --> 00:13:12,290
got a semi truck.

152
00:13:12,590 --> 00:13:18,670
So they're not there's no standard description here that's printed out.

153
00:13:18,770 --> 00:13:21,130
What kind of going to that here in just a second.

154
00:13:21,650 --> 00:13:24,830
But if I try to do something like vehicle dot

155
00:13:30,200 --> 00:13:40,830
blow air horn I get an error here because the top vehicle has no memory blower horn.

156
00:13:40,850 --> 00:13:48,140
So basically we could use any of these things that we define our protocol but we extended our Sumatra

157
00:13:48,230 --> 00:13:50,180
and added this blower horn function.

158
00:13:50,180 --> 00:13:50,810
Right.

159
00:13:51,940 --> 00:13:57,230
So this being a top vehicle it knows nothing about blower horn.

160
00:13:57,430 --> 00:14:00,990
If we wanted to use this in this array as we walk it.

161
00:14:01,150 --> 00:14:12,750
So if we did something like gef let the cool cool be cool as a semi truck then we can say we call it

162
00:14:12,980 --> 00:14:15,120
blow air horn.

163
00:14:15,460 --> 00:14:17,140
Now you can see down here in the car.

164
00:14:17,150 --> 00:14:20,210
So we get to blow air horn.

165
00:14:20,320 --> 00:14:26,830
So basically we look through this right car was not a semi truck so it skipped over the next element

166
00:14:26,830 --> 00:14:30,810
was truck and it blew it blew the air horn.

167
00:14:30,830 --> 00:14:32,000
So that's pretty cool.

168
00:14:34,380 --> 00:14:38,750
In addition to that protocols can be combined.

169
00:14:39,360 --> 00:14:49,100
So there is a protocol called custom screen convertible that a lot of types conform to.

170
00:14:49,920 --> 00:14:53,130
And I'm going to editing it here.

171
00:14:53,130 --> 00:14:56,110
Click on it and we'll go look at the implementation.

172
00:14:57,260 --> 00:15:02,540
So you say here this is a protocol custom inconvertible what's in the standard library and it gives

173
00:15:02,540 --> 00:15:05,300
a textual representation of the instance.

174
00:15:05,510 --> 00:15:10,760
And basically when you conform to this you have to add this property here description and it's just

175
00:15:10,760 --> 00:15:12,410
a good property.

176
00:15:12,440 --> 00:15:19,780
And so whenever you call the description on your object you give it a friendly name to print out or

177
00:15:19,810 --> 00:15:21,040
to return.

178
00:15:21,560 --> 00:15:24,620
So let's go back and let's implement this.

179
00:15:24,620 --> 00:15:31,080
So this protocol is in the standard library so we're just going to add it to our vehicle protocol here

180
00:15:31,290 --> 00:15:38,970
in the way you do that is sort of how you either conform to the protocol or you inherit from a superclass.

181
00:15:38,970 --> 00:15:43,260
So you just put your colon here and then we add custom string convertible.

182
00:15:43,480 --> 00:15:49,050
And now we're getting in the air here because it does not conform to it.

183
00:15:49,180 --> 00:15:50,500
So now we need to add

184
00:15:53,480 --> 00:15:54,360
description.

185
00:15:54,420 --> 00:16:00,810
So we need to add description into our sports car and this is we're going to return

186
00:16:04,440 --> 00:16:05,680
we'll see

187
00:16:12,010 --> 00:16:14,130
yeah let's do it this way.

188
00:16:14,340 --> 00:16:21,810
So we'll say if it is running we'll present sports car

189
00:16:24,060 --> 00:16:26,120
currently running.

190
00:16:26,790 --> 00:16:27,690
Otherwise

191
00:16:30,360 --> 00:16:36,580
we'll present.

192
00:16:37,380 --> 00:16:40,890
And then we'll do the same thing down here in Sumatra.

193
00:16:41,310 --> 00:16:45,450
And you say we don't have to specify because some steering convertible here because we specified it

194
00:16:46,230 --> 00:16:48,040
in our vehicle protocol.

195
00:16:48,570 --> 00:16:56,910
Now we could take this out and add it.

196
00:16:57,040 --> 00:16:59,240
Let's see we implemented it here.

197
00:16:59,240 --> 00:17:04,190
So so now we're getting this error on this description property here we could just put a comma in the

198
00:17:04,220 --> 00:17:28,140
custom string convertible.

199
00:17:28,200 --> 00:17:32,030
Sorry we don't need a print here.

200
00:17:32,040 --> 00:17:33,010
Wanting a return

201
00:17:42,150 --> 00:17:44,060
are sort of taken care of.

202
00:17:46,920 --> 00:17:47,360
All right.

203
00:17:47,420 --> 00:17:48,900
So their goals are.

204
00:17:49,340 --> 00:17:49,960
So now the

205
00:17:52,890 --> 00:17:59,160
now that we've got that now we've got it in the sports car that functionality that we don't have in

206
00:17:59,160 --> 00:18:00,000
Sumatra.

207
00:18:01,220 --> 00:18:07,220
And then again if we wanted to make it available to every vehicle we just go ahead and put it back in

208
00:18:07,220 --> 00:18:17,930
our protocol peer when we do that we see right here that need implemented in our in our semi truck as

209
00:18:17,930 --> 00:18:18,230
well.

210
00:18:18,230 --> 00:18:36,010
So if it is running and we need to return not pregnant or semi truck currently running otherwise it

211
00:18:36,510 --> 00:18:42,630
turns Sumatra currently shut down.

212
00:18:44,720 --> 00:18:45,320
OK.

213
00:18:45,400 --> 00:18:46,420
And that takes care of that.

214
00:18:46,420 --> 00:18:50,980
So now the when we loop through this we'll take this code out of here.

215
00:18:53,210 --> 00:18:59,450
We'll just come back we'll look through and we'll print the vehicle description because it's tied in

216
00:18:59,460 --> 00:19:02,270
with both of them now so we'll say

217
00:19:06,600 --> 00:19:07,350
print

218
00:19:13,040 --> 00:19:16,390
the vehicle description.

219
00:19:16,560 --> 00:19:22,480
And so now down here in the car so you can see we've got a sports car currently turned off in a semi

220
00:19:22,480 --> 00:19:24,610
truck currently shut down.

221
00:19:25,480 --> 00:19:31,710
So now if we come up here we've got them both turned off if we just take these out.

222
00:19:31,900 --> 00:19:32,730
They should change it.

223
00:19:32,730 --> 00:19:38,510
Now you can say our sports car is currently running in semi truck is currently running so we could just

224
00:19:38,510 --> 00:19:40,150
shut one of them down.

225
00:19:42,730 --> 00:19:45,170
So we could turn off truck one.

226
00:19:45,260 --> 00:19:48,140
Now you can see our semi trucks currently shut down.

227
00:19:48,910 --> 00:19:55,660
So that kind of covers protocols in how you can form to them you still have to provide all the implementation

228
00:19:55,660 --> 00:19:56,410
code.

229
00:19:56,800 --> 00:20:02,830
And like I said that's where the magic happens is when you start doing protocol extensions you can actually

230
00:20:02,830 --> 00:20:05,120
provide some of that implementation.

231
00:20:05,120 --> 00:20:07,500
And I'll talk about why the next lesson.

232
00:20:07,750 --> 00:20:15,110
But that's our introduction to protocols and we'll continue to put it together in the next lesson.

233
00:20:15,130 --> 00:20:16,310
Thanks for watching.

234
00:20:16,450 --> 00:20:17,900
We'll see you next time.

235
00:20:17,920 --> 00:20:18,120
Ligers.
