1
00:00:08,150 --> 00:00:11,110
Everybody what's going on this is Caleb with devah slopes dot com.

2
00:00:11,130 --> 00:00:15,540
And in this video we're going to start talking about protocols and delegates.

3
00:00:15,540 --> 00:00:15,990
OK.

4
00:00:16,290 --> 00:00:24,090
Protocols are really really amazing feature of swift particularly but of couple other programming languages.

5
00:00:24,090 --> 00:00:26,410
And basically what a protocol is is.

6
00:00:26,430 --> 00:00:27,890
Think of it like a blueprint.

7
00:00:28,020 --> 00:00:28,480
OK.

8
00:00:28,740 --> 00:00:35,940
Now this blueprint can contain methods or functions properties and other requirements that fit a particular

9
00:00:35,940 --> 00:00:38,950
task or like a piece of functionality.

10
00:00:39,000 --> 00:00:47,010
And what we're able to do is we're able to conform to a certain protocol from a class form from a structure

11
00:00:47,280 --> 00:00:50,780
or even an enumeration which you'll see later in this video.

12
00:00:51,120 --> 00:00:58,320
And that enumeration or that structure or that class can provide the actual implementation of the methods

13
00:00:58,320 --> 00:01:00,840
or properties or whatever you set in your protocol.

14
00:01:00,840 --> 00:01:02,410
So really really cool.

15
00:01:02,430 --> 00:01:04,100
Let's dive into it now.

16
00:01:04,290 --> 00:01:08,840
You're going to want to open up X code beta and we're going to get started with a playground.

17
00:01:08,960 --> 00:01:11,020
We pull it open in the right window.

18
00:01:11,030 --> 00:01:20,290
Here we go and just click blank and I'm going to actually let's put this in the proper folder for protocols.

19
00:01:20,370 --> 00:01:24,960
Call this protocol mania.

20
00:01:24,960 --> 00:01:26,240
That sounds fun.

21
00:01:26,370 --> 00:01:27,430
Press create.

22
00:01:27,840 --> 00:01:28,910
And here we go.

23
00:01:29,150 --> 00:01:33,530
OK let me bump up the font size a bit and let's get rid of this.

24
00:01:33,540 --> 00:01:35,130
We do need you like it.

25
00:01:35,280 --> 00:01:37,140
We don't need that string.

26
00:01:37,140 --> 00:01:37,880
And great.

27
00:01:37,950 --> 00:01:39,570
OK let's let's get after it.

28
00:01:39,570 --> 00:01:46,860
So let me start by showing you the protocol syntax which you can use by simply typing protocol and then

29
00:01:46,860 --> 00:01:49,350
giving a name to your protocol.

30
00:01:49,380 --> 00:01:55,060
Now it's the same thing that you would do for naming a class or an enumeration or a structure.

31
00:01:55,350 --> 00:02:01,230
All capital first letters of all of the words so if there's three words there's three capital letters

32
00:02:02,040 --> 00:02:04,890
for this one we're just going to call it number and k.

33
00:02:04,950 --> 00:02:09,390
And we're going to be doing some fun things with different types of numbers and I'm just going to show

34
00:02:09,390 --> 00:02:12,330
you how protocols can be valuable.

35
00:02:12,480 --> 00:02:18,000
Then we're going to do another example using some classes and a couple of other things to show you how

36
00:02:18,000 --> 00:02:23,970
it can be used in class instances and then in the next couple of videos we're going to use protocols

37
00:02:24,000 --> 00:02:27,930
as a means of delegating information between View controllers and lots of really cool stuff.

38
00:02:27,930 --> 00:02:32,320
So let's just begin by getting familiar with protocols in general.

39
00:02:32,370 --> 00:02:38,830
So inside of a protocol you can include methods and you can also include properties.

40
00:02:38,850 --> 00:02:44,140
So I'm going to include a property in this protocol called a float value and I'm going to declare it

41
00:02:44,150 --> 00:02:48,300
by typing var float value of type float.

42
00:02:48,600 --> 00:02:55,110
And if we want this property to be accessible we can go ahead and give it some curly braces and then

43
00:02:55,110 --> 00:03:01,730
inside these curly braces we can choose to put get or get set.

44
00:03:01,980 --> 00:03:09,470
And that will allow us to either just get the value of the flow value or be able to set it as well.

45
00:03:09,510 --> 00:03:14,540
But for this video we're only going to need to get the value and I'll show you why later.

46
00:03:14,550 --> 00:03:20,550
Now let's say that we wanted to include a function as well maybe get flow value ok.

47
00:03:20,880 --> 00:03:29,010
You just declare it like this func get float value and you include the parentheses for showing that

48
00:03:29,010 --> 00:03:34,650
it's a function but you're not going to put the curly braces and watch what happens when I do that protocol

49
00:03:34,650 --> 00:03:36,000
methods may not have bodies.

50
00:03:36,000 --> 00:03:42,530
Remember the description I said at the beginning of the video the protocol is not where you declare

51
00:03:42,540 --> 00:03:43,850
the implementation.

52
00:03:43,950 --> 00:03:49,650
OK wherever you conform to this protocol later on that's where you'll declare the implementation and

53
00:03:49,650 --> 00:03:51,530
I'll show you what I mean.

54
00:03:51,570 --> 00:03:56,960
But for now just get rid of this function and leave the variable float value.

55
00:03:56,970 --> 00:04:03,720
So what we can do with this protocol now is we can conform to it just like you would with subclassing.

56
00:04:03,720 --> 00:04:10,410
So if I wanted to create a square class and then create maybe a rectangle subclass that inherited from

57
00:04:10,410 --> 00:04:18,890
square I could do that by typing class square whip's where and then.

58
00:04:18,960 --> 00:04:23,640
Sorry I meant class Rectangle and I could inherit from class Square.

59
00:04:23,670 --> 00:04:25,120
That's one way I could do it.

60
00:04:25,320 --> 00:04:27,880
So for protocol's it's kind of the same thing.

61
00:04:28,170 --> 00:04:31,660
I'm going to make an extension of float.

62
00:04:31,770 --> 00:04:32,070
OK.

63
00:04:32,070 --> 00:04:37,070
Now if you've already seen the extension video that's great so you'll have some familiarity with extensions.

64
00:04:37,110 --> 00:04:44,120
I'm going to extend the float type and I'm going to conform to my protocol number.

65
00:04:44,130 --> 00:04:51,840
Now you'll notice there's going to be an issue that says type float does not conform to protocol number.

66
00:04:51,840 --> 00:04:54,980
Now remember I said that a protocol is sort of like a blueprint.

67
00:04:55,040 --> 00:04:55,230
Right.

68
00:04:55,230 --> 00:04:59,480
A set of instructions in order to set something up the right way.

69
00:04:59,490 --> 00:05:07,260
So what we're saying is if we conform to the protocol number we need to include the variable float value

70
00:05:07,920 --> 00:05:14,760
what we can do to do that is we can basically call float value and you'll notice that it shows up right

71
00:05:14,760 --> 00:05:19,410
there and my list of properties if I press Enter you'll see that it passes it in.

72
00:05:19,590 --> 00:05:25,530
But what we need to do is we need to return a certain value to this variable so we can do that by typing

73
00:05:25,530 --> 00:05:26,430
return.

74
00:05:26,730 --> 00:05:31,140
And let's think about what do we need to return an extension of float.

75
00:05:31,230 --> 00:05:34,370
We need to return whatever value is passed in as a float.

76
00:05:34,380 --> 00:05:40,150
So let's say I create a variable of type float and I should return self to float value.

77
00:05:40,200 --> 00:05:43,230
Right because it already is of type float.

78
00:05:43,230 --> 00:05:50,850
So that's great and all but that basically is going to give us the ability to pull out the float value

79
00:05:50,850 --> 00:05:52,680
of a certain constant or variable.

80
00:05:52,680 --> 00:05:59,840
So like for instance I could do this let 4 of type float equals four.

81
00:05:59,880 --> 00:06:04,080
And now if I wanted to get the explicit float value I could type for.

82
00:06:04,290 --> 00:06:11,310
And as you can see we can pull out the float value in case we've extended float to include the property

83
00:06:11,310 --> 00:06:12,330
float value.

84
00:06:12,330 --> 00:06:13,270
Pretty cool.

85
00:06:13,590 --> 00:06:24,120
So now let's think if I wanted to do some math maybe between of our three of type int equals 3 and var

86
00:06:24,150 --> 00:06:30,570
4 of type float equals 4 if I wanted to do three plus four.

87
00:06:30,870 --> 00:06:32,380
I'm going to get an error.

88
00:06:32,460 --> 00:06:38,760
And the reason for that is because the types are not compatible int and float are not the same so we

89
00:06:38,760 --> 00:06:41,360
can't add them because it doesn't know how to do that.

90
00:06:41,400 --> 00:06:48,600
But what we can do is we can write an extension of all of our number types and we can give it the float

91
00:06:48,600 --> 00:06:52,830
value property that's going to essentially convert it into a float.

92
00:06:52,830 --> 00:06:53,910
Let's do that.

93
00:06:53,910 --> 00:07:00,690
So let's go ahead and type extension and let's extend double because that's one of our number types.

94
00:07:00,690 --> 00:07:07,050
So we're going to conform to the protocol number and then to officially conform we need to include all

95
00:07:07,050 --> 00:07:07,800
the properties.

96
00:07:07,800 --> 00:07:12,030
So let's go ahead and give it the value for float value.

97
00:07:12,360 --> 00:07:16,260
And now we need to think what are we going to return this time.

98
00:07:16,500 --> 00:07:21,900
We need to return but we need to think if a double is coming in.

99
00:07:21,960 --> 00:07:28,350
We need to somehow convert that into a float and how we can do that is by doing typecasting.

100
00:07:28,380 --> 00:07:35,820
So go ahead and type float and we're going to give it self meaning whatever double value we have.

101
00:07:35,970 --> 00:07:44,050
We can pass that in itself and we can encapsulate it as a float and return that to to our variable.

102
00:07:44,250 --> 00:07:48,750
So instead of doing three let's let's change that to a double.

103
00:07:48,810 --> 00:07:51,630
It's still not going to work because they're not related types.

104
00:07:51,690 --> 00:07:59,730
But what I can do now is I can type three float value and I can add that to for now type compatible.

105
00:07:59,730 --> 00:08:06,360
So if I actually were to do this it would print out three plus four equals seven successfully and it

106
00:08:06,360 --> 00:08:12,480
would be of type float because using my extension I can now pull out the specific float value for that

107
00:08:12,480 --> 00:08:13,110
variables.

108
00:08:13,110 --> 00:08:14,760
Pretty awesome.

109
00:08:14,760 --> 00:08:22,290
Let's go ahead and let's get rid of that and let's keep extending this extension of not of type number

110
00:08:22,290 --> 00:08:24,970
but we're conforming to the number protocol.

111
00:08:25,260 --> 00:08:29,360
Let's go ahead and call float value and let's return just like before.

112
00:08:29,360 --> 00:08:32,270
Float.

113
00:08:32,340 --> 00:08:36,200
We're going to encapsulate self and cast it as a float.

114
00:08:36,210 --> 00:08:42,300
Next we're going to go ahead and call extension you and because there are many times where we will have

115
00:08:42,420 --> 00:08:53,120
you int values go ahead and conform to the number protocol call float value and return float self.

116
00:08:53,370 --> 00:08:55,600
Self encapsulated as a float.

117
00:08:55,620 --> 00:09:00,180
So now we have four extensions we can access the float value of any of these.

118
00:09:00,180 --> 00:09:02,120
Now which is really cool.

119
00:09:02,190 --> 00:09:07,650
And now let's do our best to write an equation that's going to let us basically add numbers of unrelated

120
00:09:07,650 --> 00:09:08,230
types.

121
00:09:08,280 --> 00:09:14,820
So let's go ahead and let's create a variable called X and let's make it of type double and let's make

122
00:09:14,820 --> 00:09:18,220
sure that it's a double by adding some decimals at the end.

123
00:09:18,450 --> 00:09:26,910
Let's create another value called Y of type int and let's go ahead make that equal to 5 because it's

124
00:09:26,910 --> 00:09:29,200
an integer it must be whole value.

125
00:09:29,400 --> 00:09:32,090
And let's go ahead and let.

126
00:09:32,110 --> 00:09:33,930
Q I'm going to divide these.

127
00:09:33,930 --> 00:09:40,230
Let Q be the quotient and it's going to be equal to x divided by Y.

128
00:09:40,230 --> 00:09:42,590
OK let's see what happens.

129
00:09:42,660 --> 00:09:44,040
It looks like we're getting an error here.

130
00:09:44,040 --> 00:09:44,730
Check it out.

131
00:09:44,910 --> 00:09:45,840
It's saying

132
00:09:48,840 --> 00:09:56,280
the binary operator divide cannot be applied to operands of type double and int k so we're getting the

133
00:09:56,280 --> 00:09:58,370
same problem as before.

134
00:09:58,470 --> 00:10:02,670
But what we're going to do is we're going to write some functions here that will allow us to make these

135
00:10:02,670 --> 00:10:03,630
work nicely.

136
00:10:03,630 --> 00:10:09,900
Now one thing we could do is we could do X float value divided by wide float value.

137
00:10:09,910 --> 00:10:12,960
But what if we don't think to pull that out all the time.

138
00:10:13,150 --> 00:10:14,150
That might not work.

139
00:10:14,230 --> 00:10:17,230
So as you can see it does work to do this.

140
00:10:17,380 --> 00:10:21,410
But what if we just wanted to be able to divide unrelated types.

141
00:10:21,670 --> 00:10:27,480
We can write some functions that are going to allow us to override how these binary operators work.

142
00:10:27,490 --> 00:10:29,410
Let's do that really quick it's pretty cool.

143
00:10:29,650 --> 00:10:37,480
So go ahead and we're going to basically just write phunk add K and inside of add we're going to pass

144
00:10:37,480 --> 00:10:48,190
in two values right value a of type number k and value B of type number and we're using our protocol

145
00:10:48,190 --> 00:10:50,830
there is kind of like a class to pass in.

146
00:10:50,830 --> 00:10:57,940
Now inside this function if we're adding we're going to take value a whatever we pass in float value

147
00:10:58,660 --> 00:11:03,420
and we're gonna go ahead and add it to value be float value.

148
00:11:04,270 --> 00:11:09,670
The only issue is that we actually need to return a value here K and we're going to return a value of

149
00:11:09,670 --> 00:11:10,650
type float.

150
00:11:10,810 --> 00:11:17,590
Let's say we're just working with floats so we can return it like so return value a plus float value

151
00:11:18,010 --> 00:11:21,270
plus value B plus foot dot float value.

152
00:11:21,640 --> 00:11:23,190
So many so many floats there.

153
00:11:23,190 --> 00:11:23,600
OK.

154
00:11:23,770 --> 00:11:27,820
So that is how we can do the addition function.

155
00:11:28,000 --> 00:11:32,780
But what we're going to do is we're going to do this for addition subtraction division and multiplication.

156
00:11:32,780 --> 00:11:36,770
So copy this function just for time we're going to paste it a few times.

157
00:11:37,000 --> 00:11:42,910
I wouldn't normally do this but just for time time's sake we're going to replace plus with minus and

158
00:11:43,000 --> 00:11:50,410
do that again replace plus here with Divide and replace it there as well and then replace this with

159
00:11:50,410 --> 00:11:54,050
an asterisk for multiplication and do the same thing there.

160
00:11:54,050 --> 00:11:59,740
Now we have these functions whenever we call Plus it's going to use it like a function.

161
00:11:59,740 --> 00:12:01,400
We're going to take value a.

162
00:12:01,560 --> 00:12:04,410
Pass it in value B pass it in.

163
00:12:04,420 --> 00:12:06,370
Add them together and return the value.

164
00:12:06,370 --> 00:12:08,320
So now watch what happens.

165
00:12:08,320 --> 00:12:12,060
I have two unrelated types double and end.

166
00:12:12,460 --> 00:12:18,670
But when I use the division operand check out what happens it passes in these two values and it returns

167
00:12:18,670 --> 00:12:19,710
a float.

168
00:12:19,780 --> 00:12:20,490
It works.

169
00:12:20,500 --> 00:12:27,130
I am able to do operations on two unrelated types thanks to protocol's super super cool.

170
00:12:27,130 --> 00:12:34,330
So as you can see protocols are pretty awesome they allow us to basically set a blueprint for values

171
00:12:34,330 --> 00:12:37,010
that we are going to use or we'll need to use.

172
00:12:37,150 --> 00:12:40,090
And this is just one way they can be used.

173
00:12:40,150 --> 00:12:42,840
I think I'm actually going to split this video into two.

174
00:12:42,910 --> 00:12:48,490
And in the next video we're going to actually see how protocols can benefit us when using classes or

175
00:12:48,490 --> 00:12:50,530
structures et cetera et cetera.

176
00:12:50,530 --> 00:12:55,050
So let's go ahead and let's move over to the next video and I'll show you how to do that right now.

