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