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