1
00:00:08,090 --> 00:00:12,630
Hey everybody what's going on this is Caleb with slopes dot com and in this video we're going to continue

2
00:00:12,630 --> 00:00:14,870
talking about protocols and delegates.

3
00:00:15,090 --> 00:00:21,390
Except this time we're going to see how they can be useful in classes and structures specifically classes.

4
00:00:21,400 --> 00:00:24,820
But the same principles apply to structures as well.

5
00:00:24,840 --> 00:00:30,630
So we're going to remain in the same playground as the last video but I'm going to make some space just

6
00:00:30,750 --> 00:00:33,970
so that it's like we're using a new one.

7
00:00:34,020 --> 00:00:39,810
Go ahead and we're going to actually begin by creating kind of a quiz generator that can generate a

8
00:00:39,810 --> 00:00:43,740
random question and we're going to write a class for a question.

9
00:00:43,770 --> 00:00:48,900
OK we're going to give it a few properties and initialiser and then we're going to create some incomes

10
00:00:48,930 --> 00:00:51,710
for question types and answer types.

11
00:00:51,840 --> 00:00:55,790
So let's go ahead and let's just begin with our class 4 question.

12
00:00:55,860 --> 00:01:03,920
Go ahead and type wups class question and then we need some properties here we need a question type.

13
00:01:03,930 --> 00:01:05,940
So go ahead and type of var type.

14
00:01:06,360 --> 00:01:10,200
And that's going to be of type question type which we have not yet created.

15
00:01:10,200 --> 00:01:12,090
We will below in just a moment.

16
00:01:12,270 --> 00:01:18,480
Go ahead and type var queery of type string and var answer.

17
00:01:18,530 --> 00:01:21,900
OK those are the things that every question should have below.

18
00:01:21,960 --> 00:01:32,400
Go ahead and give it an initializer and pass in a type of question question type a query of type string

19
00:01:32,610 --> 00:01:36,670
and answer of type string as well.

20
00:01:36,730 --> 00:01:40,330
Go ahead and set them up by typing self-deceit type equals type.

21
00:01:40,350 --> 00:01:43,650
Of course that's not going to work because we haven't created the question type.

22
00:01:43,650 --> 00:01:50,830
ENM self-talk queery equals queery and self-taught.

23
00:01:50,850 --> 00:01:53,370
Answer equals answer.

24
00:01:53,460 --> 00:01:57,910
Now it's going to yell at us until we create our question type ENM.

25
00:01:57,930 --> 00:01:59,250
So let's do that now.

26
00:01:59,350 --> 00:02:04,260
OK so below are a class type ENM question type K.

27
00:02:04,290 --> 00:02:07,280
And it's going to be of type string.

28
00:02:07,380 --> 00:02:10,100
Because each question should have a string value right.

29
00:02:10,110 --> 00:02:12,500
That's where the words of the question will come.

30
00:02:12,690 --> 00:02:15,680
And we're going to create four different question types here.

31
00:02:15,690 --> 00:02:17,090
Go ahead and type case.

32
00:02:17,130 --> 00:02:22,250
True false case multiple choice.

33
00:02:22,630 --> 00:02:25,740
Oops that's with A's C case.

34
00:02:25,740 --> 00:02:27,510
Short answer.

35
00:02:30,120 --> 00:02:33,440
In case essay four different kinds of questions.

36
00:02:33,570 --> 00:02:38,940
And let's go ahead and let's give these each an explicit value just for this example by typing equals

37
00:02:39,090 --> 00:02:43,450
and a string here and we're going to just say is the sky blue.

38
00:02:43,740 --> 00:02:46,250
There's my true false question.

39
00:02:46,300 --> 00:02:48,320
Wait that's not a question.

40
00:02:48,370 --> 00:02:52,330
How about the sky is blue.

41
00:02:52,350 --> 00:02:53,990
True or false.

42
00:02:54,630 --> 00:02:57,820
Multiple choice we're going to give some options here.

43
00:02:57,840 --> 00:03:02,400
Who is the ugliest Beetle.

44
00:03:02,610 --> 00:03:08,270
Well I guess Colin John Paul George or Ringo.

45
00:03:08,700 --> 00:03:11,150
I have my opinion but I'm not going to say it.

46
00:03:11,160 --> 00:03:17,680
Short answer will be what is the capital of Oregon my home state.

47
00:03:18,300 --> 00:03:19,390
And for the essay.

48
00:03:19,550 --> 00:03:21,710
Let's give a nice really easy question here.

49
00:03:21,720 --> 00:03:31,690
How about in 50 whips in 50 words explain molecular fusion.

50
00:03:31,800 --> 00:03:32,430
Easy.

51
00:03:32,750 --> 00:03:33,450
OK great.

52
00:03:33,450 --> 00:03:35,610
So there's our question types.

53
00:03:35,610 --> 00:03:37,960
Next we're going to need answer types as well.

54
00:03:37,980 --> 00:03:43,800
So we're going to just create static answers for these questions and let's do that by creating an almost

55
00:03:43,800 --> 00:03:45,930
identical enumeration called answer type.

56
00:03:45,930 --> 00:03:54,360
So go ahead type in answer type of type string and we're going to create the same cases case true false

57
00:03:55,110 --> 00:04:00,560
case multiple choice case.

58
00:04:00,630 --> 00:04:05,440
Short answer and case essay.

59
00:04:05,460 --> 00:04:08,490
Now we're going to give these explicit values as well.

60
00:04:08,520 --> 00:04:11,340
The sky is blue is going to be true.

61
00:04:12,630 --> 00:04:13,590
Multiple choice.

62
00:04:13,590 --> 00:04:21,030
We're going to say Sergeant Pepper we're going to obviously get that wrong.

63
00:04:21,020 --> 00:04:23,990
We're going to say the short answer is Salem.

64
00:04:24,090 --> 00:04:25,050
If you knew that.

65
00:04:25,140 --> 00:04:26,160
Bonus points for you.

66
00:04:26,160 --> 00:04:27,240
Ding ding.

67
00:04:27,360 --> 00:04:40,530
And the essay will say molecular fusion happens when a daddy molecule and a mommy molecule love each

68
00:04:40,530 --> 00:04:45,090
other very much.

69
00:04:45,110 --> 00:04:50,660
It's not really how it works but that's just my my my thought on how it works anyway.

70
00:04:51,060 --> 00:04:54,170
So we have four question types for answer types.

71
00:04:54,300 --> 00:05:00,420
They have explicit values and we can initialise a question K of type question.

72
00:05:00,510 --> 00:05:02,200
So pretty cool.

73
00:05:02,250 --> 00:05:07,260
Now what we're going to do is we're going to create a protocol and we're going to give it a method and

74
00:05:07,320 --> 00:05:11,850
it's going to basically be a blueprint for our question generator.

75
00:05:11,970 --> 00:05:13,750
Let's say we're building a quiz app.

76
00:05:14,010 --> 00:05:21,300
And of course this data would probably come in from an API pulling random questions but we need to basically

77
00:05:21,300 --> 00:05:26,130
set up a blueprint that's going to allow us to generate questions when we need to.

78
00:05:26,130 --> 00:05:34,780
So go ahead and type protocol question generator and inside our protocol we're going to declare a method.

79
00:05:34,900 --> 00:05:43,320
OK so go ahead and type phunk generate random question and we're going to actually have this return

80
00:05:43,380 --> 00:05:49,590
a question to us but remember we don't have any kind of implementation inside this protocol so we don't

81
00:05:49,590 --> 00:05:53,010
need to put curly brackets we don't need to write any code.

82
00:05:53,070 --> 00:05:55,230
The protocol is just the blueprint.

83
00:05:55,230 --> 00:06:03,090
So basically this this helps us to include what we actually need and to include the kinds of functions

84
00:06:03,090 --> 00:06:05,940
that are required for a certain feature.

85
00:06:06,000 --> 00:06:13,080
So if we conform to the question generator protocol maybe in a quiz class later on.

86
00:06:13,080 --> 00:06:18,480
It basically reminds us hey you need to be able to generate questions for this quiz and it's just a

87
00:06:18,480 --> 00:06:25,540
way to kind of compartmentalize and put features in a certain place where they need to go.

88
00:06:25,590 --> 00:06:30,660
So now that we have our protocol here and we have the function generate random question that returns

89
00:06:30,660 --> 00:06:35,020
a question we can go ahead and create a class for our quiz case.

90
00:06:35,040 --> 00:06:41,010
So go ahead and type class quiz and we're going to conform to the quiz generator

91
00:06:44,040 --> 00:06:45,650
protocol like so.

92
00:06:45,990 --> 00:06:46,470
OK.

93
00:06:46,590 --> 00:06:50,350
But it's going to throw an error at us it's going to say hey we don't have that type.

94
00:06:50,400 --> 00:06:52,560
And that's because.

95
00:06:52,950 --> 00:06:55,380
Oh no wonder that's because it's a question generator.

96
00:06:55,800 --> 00:06:56,190
OK.

97
00:06:56,190 --> 00:06:56,760
There we go.

98
00:06:56,760 --> 00:07:01,860
Question generator and it's going to throw an error at me saying that we have not yet conformed to question

99
00:07:01,860 --> 00:07:02,650
generator.

100
00:07:02,880 --> 00:07:05,540
That's because we are missing our blueprint instructions.

101
00:07:05,550 --> 00:07:07,300
Generate random question.

102
00:07:07,410 --> 00:07:13,980
So inside of this class I'm going to go ahead and I'm going to call generate random question and you'll

103
00:07:13,980 --> 00:07:20,760
notice the error goes away because we have added all the functions that question generator says we need.

104
00:07:20,760 --> 00:07:24,160
We have not yet done anything with them but we've at least called the function.

105
00:07:24,360 --> 00:07:29,500
So that's cool but now we need to be able to generate a random in here.

106
00:07:29,670 --> 00:07:32,010
So let's go ahead and let's think.

107
00:07:32,160 --> 00:07:39,660
I want to basically choose a random question type and then I'm going to basically pass in the values

108
00:07:39,660 --> 00:07:44,600
needed to generate a question which is a type a query and an answer.

109
00:07:44,610 --> 00:07:49,220
So let's go ahead and let's generate a random number first.

110
00:07:49,290 --> 00:07:59,390
OK so let's just say let Rand numeral equals and we're going to use arke for random uniform.

111
00:07:59,700 --> 00:08:02,360
And we're going to set an upper bounds of 4.

112
00:08:02,640 --> 00:08:02,910
OK.

113
00:08:02,910 --> 00:08:08,490
Now that's going to generate numbers between 0 1 2 and 3 K for values which is perfect when dealing

114
00:08:08,490 --> 00:08:11,430
with the index of something.

115
00:08:11,430 --> 00:08:13,080
So that's really cool.

116
00:08:13,080 --> 00:08:19,590
The issue though is that we're actually going to need this to be of type int and not type you and if

117
00:08:19,590 --> 00:08:24,020
you look you'll notice that arc for random uniform returns a 32.

118
00:08:24,030 --> 00:08:29,940
So let's go ahead and type in it and we're going to pass our arc for random uniform function into that

119
00:08:29,940 --> 00:08:31,920
so that it is of type integer.

120
00:08:31,950 --> 00:08:35,470
OK so let's say this generates a number 2.

121
00:08:35,530 --> 00:08:41,550
Now in our incomes we need to be able to allow number which remember if we're dealing with an index

122
00:08:41,550 --> 00:08:44,280
number two is really the third result.

123
00:08:44,430 --> 00:08:51,300
So that would be short answer but an enum in and of itself does not have any kind of indexing system

124
00:08:51,300 --> 00:08:54,810
or a way to identify first second third fourth.

125
00:08:54,810 --> 00:09:00,810
So what we're going to actually do is inside of our Inam we're going to create an array and we're going

126
00:09:00,810 --> 00:09:05,970
to basically just let We're going to pass in all of our types so that we can order them in some type

127
00:09:05,970 --> 00:09:06,890
of way.

128
00:09:06,960 --> 00:09:13,230
So we're going to go ahead and just type let types and we're going to go ahead and pass in true false

129
00:09:13,260 --> 00:09:18,990
WIPs true false multiple choice.

130
00:09:19,020 --> 00:09:22,250
Short answer and essay.

131
00:09:22,640 --> 00:09:23,290
OK.

132
00:09:23,640 --> 00:09:31,380
And this this array is going to allow us to parse through and I can pull out the index here at index

133
00:09:31,380 --> 00:09:37,410
1 and I can pull out multiple choice K that's going to be how we identify the different types that we

134
00:09:37,410 --> 00:09:37,760
want.

135
00:09:37,770 --> 00:09:39,090
But we're getting an error here.

136
00:09:39,090 --> 00:09:40,020
Check it out.

137
00:09:40,180 --> 00:09:47,190
Anime's may not contain stored properties in order to circumvent this we can create this as a static

138
00:09:47,220 --> 00:09:48,140
constant.

139
00:09:48,350 --> 00:09:48,900
OK.

140
00:09:49,260 --> 00:09:54,870
Now a static constant is basically going to allow this to exist for the entire life cycle of the app

141
00:09:55,380 --> 00:09:56,580
since it's static.

142
00:09:56,850 --> 00:10:01,560
And we're going to use that because we need to be able to access it all the time throughout this when

143
00:10:01,560 --> 00:10:02,820
this code runs.

144
00:10:02,820 --> 00:10:08,580
Now this is great for questions but we're also going to need it for our answers as well so I copied

145
00:10:08,580 --> 00:10:10,650
it and I'm pasting it down below.

146
00:10:10,770 --> 00:10:12,730
So we have a random number.

147
00:10:12,780 --> 00:10:18,060
Now we're going to need to use this number to basically pull out a random type from our Random type's

148
00:10:19,170 --> 00:10:20,960
array inside of our enumeration.

149
00:10:20,970 --> 00:10:28,140
So go ahead and type let random type equals question type and what we're going to do is we're going

150
00:10:28,140 --> 00:10:33,670
to pull out our type's array and we're going to pass in random Numeral.

151
00:10:34,350 --> 00:10:38,700
Now let's say that was two or three whatever it is it's going to pull out a random one.

152
00:10:38,710 --> 00:10:41,610
Now as it runs you'll see but we're getting an error here.

153
00:10:41,850 --> 00:10:44,420
And it's saying that we have not yet returned a question.

154
00:10:44,430 --> 00:10:47,960
That's because we have not yet returned the question so let's keep building it.

155
00:10:47,970 --> 00:10:51,160
Remember a question needs a type a query and an answer.

156
00:10:51,180 --> 00:11:00,980
So let's go ahead and type let a random query equals random type and we can pull out the raw value.

157
00:11:00,990 --> 00:11:05,460
Remember if I pulled out multiple choice the raw value is going to be the string here.

158
00:11:05,460 --> 00:11:06,450
That's my question.

159
00:11:06,460 --> 00:11:06,710
OK.

160
00:11:06,720 --> 00:11:08,550
Easy enough.

161
00:11:08,700 --> 00:11:10,020
So there we go.

162
00:11:10,050 --> 00:11:14,000
Let random answer equals.

163
00:11:14,130 --> 00:11:15,820
And where are we going to get the answer.

164
00:11:15,900 --> 00:11:21,360
We're going to get it from our answer type in numeration and we can pull out the specific type using

165
00:11:21,420 --> 00:11:22,410
our array.

166
00:11:22,410 --> 00:11:27,840
Now if I pulled out multiple choice here I can use that same numeral value here to pull out the multiple

167
00:11:27,840 --> 00:11:29,030
choice answer.

168
00:11:29,040 --> 00:11:30,930
So let's go ahead and let's call.

169
00:11:30,930 --> 00:11:38,790
Answer type types and I can pull out the right value using random numeral k that will get the proper

170
00:11:38,880 --> 00:11:40,920
answer back for me.

171
00:11:40,920 --> 00:11:43,840
So now that we have those three values we can create a question.

172
00:11:43,950 --> 00:11:44,990
So Type let.

173
00:11:45,000 --> 00:11:54,690
Random question equals a question and we're going to instantiate it here with our random type random

174
00:11:54,690 --> 00:11:57,770
queery and random answer.

175
00:11:58,320 --> 00:11:59,250
OK.

176
00:11:59,340 --> 00:12:04,130
Now at the very end we need to go ahead and just return our Random question.

177
00:12:04,230 --> 00:12:07,050
That's not a question of random question.

178
00:12:07,050 --> 00:12:08,290
There we go.

179
00:12:08,440 --> 00:12:14,850
Supercool so as it goes through the function will properly generate the value we need.

180
00:12:15,150 --> 00:12:18,560
But I'm noticing that it is giving us an issue here.

181
00:12:18,660 --> 00:12:23,760
It is saying we cannot convert answer type to expected argument string.

182
00:12:23,880 --> 00:12:26,150
So random answer is coming in.

183
00:12:26,310 --> 00:12:31,170
But we are just getting a random type here we are not pulling out the raw value.

184
00:12:31,190 --> 00:12:32,170
Oh that's right.

185
00:12:32,180 --> 00:12:37,490
So instead of that at the very end here I can go ahead and pull out the raw value for the item we pull

186
00:12:37,490 --> 00:12:38,590
out of that index.

187
00:12:38,790 --> 00:12:41,210
And now that error should go away.

188
00:12:41,300 --> 00:12:42,000
Really cool.

189
00:12:42,050 --> 00:12:43,760
So that's great.

190
00:12:43,760 --> 00:12:50,780
Now we have a quiz we have conformed to the question generator protocol and we have put in all the implementation

191
00:12:50,780 --> 00:12:53,880
code to generate a random question from our enumerations.

192
00:12:53,900 --> 00:12:56,600
So now if I create a quiz check it out I can type.

193
00:12:56,600 --> 00:13:00,110
Let quiz equals quiz.

194
00:13:00,740 --> 00:13:02,510
And then I can call quiz dot.

195
00:13:02,540 --> 00:13:05,970
Generate random question and watch what happens when I do that.

196
00:13:06,050 --> 00:13:09,330
It runs and it pulls out a question.

197
00:13:09,450 --> 00:13:10,530
Supercool.

198
00:13:10,580 --> 00:13:18,050
Now it pulled out a question type of essay and it pulled out an answer from our answers array.

199
00:13:18,050 --> 00:13:20,500
Now we have a type a query and an answer.

200
00:13:20,510 --> 00:13:21,850
That's so cool.

201
00:13:21,890 --> 00:13:26,630
So let's go ahead and let's actually print out what's happening here and actually what I'm going to

202
00:13:26,630 --> 00:13:32,420
do is I'm going to put this inside of a constant like so so that I can actually pull this out.

203
00:13:32,420 --> 00:13:40,160
So let's just say question type whip's type and I can pull out.

204
00:13:40,160 --> 00:13:41,770
Question.

205
00:13:41,900 --> 00:13:49,260
Question dot type K and you'll see it will print question type multiple choice.

206
00:13:49,270 --> 00:13:55,200
OK then we're going to say queery and you know what while we're at it why don't we just go ahead and

207
00:13:55,200 --> 00:13:59,380
do forward slash and to give us some space here.

208
00:13:59,610 --> 00:14:01,350
And for the queery we're going to pull out.

209
00:14:01,350 --> 00:14:02,300
Question.

210
00:14:02,490 --> 00:14:08,790
Queery and then slash and space and answer we can pull out.

211
00:14:08,900 --> 00:14:11,240
Question Answer.

212
00:14:11,520 --> 00:14:13,820
That's not an answer question that answer.

213
00:14:13,820 --> 00:14:16,840
So there we go as it prints we should get everything cool.

214
00:14:16,860 --> 00:14:18,370
Question type short answer.

215
00:14:18,370 --> 00:14:20,060
Queery what is the capital of Oregon.

216
00:14:20,070 --> 00:14:20,490
Answer.

217
00:14:20,490 --> 00:14:24,980
Salem everything shows up exactly as it should.

218
00:14:24,990 --> 00:14:27,260
This is how powerful protocols are.

219
00:14:27,270 --> 00:14:36,900
It gives us the ability to basically create requirements that must be conform to when using that protocol.

220
00:14:36,900 --> 00:14:43,590
And the cool thing is we can do a bunch of different things like lets say I wanted to create a new kind

221
00:14:43,590 --> 00:14:50,160
of protocol maybe one that generates answers and then you would have to do the questions kind of like

222
00:14:50,160 --> 00:14:50,780
Jeopardy.

223
00:14:50,880 --> 00:15:00,930
So lets just go ahead and do protocol answer generator and a protocol can actually inherit or conform

224
00:15:01,230 --> 00:15:03,210
to another kind of protocol.

225
00:15:03,270 --> 00:15:04,800
So that's really really cool.

226
00:15:05,040 --> 00:15:13,290
Now if you wanted to you could use this as a way to add additional functionality so phunk generate random

227
00:15:13,710 --> 00:15:14,730
answer.

228
00:15:15,300 --> 00:15:22,060
That could be my question and I could create a an answer type here.

229
00:15:22,290 --> 00:15:29,430
You know what just for time let's just go ahead and just create a class here of type answer just so

230
00:15:29,430 --> 00:15:30,750
that it doesn't yell at me.

231
00:15:31,020 --> 00:15:35,720
And if I wanted to I could create a class called jeopardy.

232
00:15:36,180 --> 00:15:38,730
And I can conform to my answer generator.

233
00:15:38,770 --> 00:15:41,430
OK which is really cool but you'll see it's going to yell at me.

234
00:15:41,520 --> 00:15:45,570
It's going to say hey you have not yet conformed to answer generator.

235
00:15:45,570 --> 00:15:52,260
Now I can go ahead and type generate random question or generate random answer because I'm conforming

236
00:15:52,560 --> 00:15:55,430
to that protocol through my other protocol.

237
00:15:55,440 --> 00:15:57,930
So you can do it just like you can subclass classes.

238
00:15:57,940 --> 00:16:01,900
You can sort of subclass protocols as well which is really neat.

239
00:16:01,920 --> 00:16:06,290
So let's go ahead and let's conform to generate random question.

240
00:16:06,450 --> 00:16:12,450
So let's go ahead and let's conform to generate random answer and you'll see we're still not getting

241
00:16:12,780 --> 00:16:13,710
conformance.

242
00:16:13,710 --> 00:16:15,500
And why is that.

243
00:16:15,690 --> 00:16:19,510
Because we're not generating a random question as well.

244
00:16:19,710 --> 00:16:26,400
In order to conform we have to meet the requirements of both to generate random answer and generate

245
00:16:26,400 --> 00:16:27,700
random question.

246
00:16:27,760 --> 00:16:30,110
K that's just something we need to do.

247
00:16:30,240 --> 00:16:34,500
That's part of the power of protocols you can create a bunch of nested protocols just like that's pretty

248
00:16:34,500 --> 00:16:35,370
cool.

249
00:16:35,370 --> 00:16:39,870
Now of course we're not returning anything here so it's going to yell at us but I hope you can see how

250
00:16:39,870 --> 00:16:43,700
helpful protocols can be and at least some small ways.

251
00:16:43,860 --> 00:16:48,900
And in the next couple of videos we're going to build an app that allows you to pass values back and

252
00:16:48,900 --> 00:16:53,490
forth between View controllers using protocols as a way to delegate information.

253
00:16:53,490 --> 00:16:59,250
It's called the delegate method and it's something that's commonly talked about with protocols we can

254
00:16:59,250 --> 00:17:07,590
use protocols to delegate information to other classes because remember a protocol itself does not contain

255
00:17:07,830 --> 00:17:10,570
any implementation we implemented on a controller.

256
00:17:10,620 --> 00:17:16,380
And so using conformance between two to view controllers we can use protocols as a way to delegate that

257
00:17:16,380 --> 00:17:16,950
information.

258
00:17:16,950 --> 00:17:18,170
It's really really cool.

259
00:17:18,300 --> 00:17:21,640
So let's go ahead and let's head over to the next video and let's build it out.

