Hey everybody what's going on this is Caleb with slopes dot com and in this video we're going to continue
talking about protocols and delegates.
Except this time we're going to see how they can be useful in classes and structures specifically classes.
But the same principles apply to structures as well.
So we're going to remain in the same playground as the last video but I'm going to make some space just
so that it's like we're using a new one.
Go ahead and we're going to actually begin by creating kind of a quiz generator that can generate a
random question and we're going to write a class for a question.
OK we're going to give it a few properties and initialiser and then we're going to create some incomes
for question types and answer types.
So let's go ahead and let's just begin with our class 4 question.
Go ahead and type wups class question and then we need some properties here we need a question type.
So go ahead and type of var type.
And that's going to be of type question type which we have not yet created.
We will below in just a moment.
Go ahead and type var queery of type string and var answer.
OK those are the things that every question should have below.
Go ahead and give it an initializer and pass in a type of question question type a query of type string
and answer of type string as well.
Go ahead and set them up by typing self-deceit type equals type.
Of course that's not going to work because we haven't created the question type.
ENM self-talk queery equals queery and self-taught.
Answer equals answer.
Now it's going to yell at us until we create our question type ENM.
So let's do that now.
OK so below are a class type ENM question type K.
And it's going to be of type string.
Because each question should have a string value right.
That's where the words of the question will come.
And we're going to create four different question types here.
Go ahead and type case.
True false case multiple choice.
Oops that's with A's C case.
Short answer.
In case essay four different kinds of questions.
And let's go ahead and let's give these each an explicit value just for this example by typing equals
and a string here and we're going to just say is the sky blue.
There's my true false question.
Wait that's not a question.
How about the sky is blue.
True or false.
Multiple choice we're going to give some options here.
Who is the ugliest Beetle.
Well I guess Colin John Paul George or Ringo.
I have my opinion but I'm not going to say it.
Short answer will be what is the capital of Oregon my home state.
And for the essay.
Let's give a nice really easy question here.
How about in 50 whips in 50 words explain molecular fusion.
Easy.
OK great.
So there's our question types.
Next we're going to need answer types as well.
So we're going to just create static answers for these questions and let's do that by creating an almost
identical enumeration called answer type.
So go ahead type in answer type of type string and we're going to create the same cases case true false
case multiple choice case.
Short answer and case essay.
Now we're going to give these explicit values as well.
The sky is blue is going to be true.
Multiple choice.
We're going to say Sergeant Pepper we're going to obviously get that wrong.
We're going to say the short answer is Salem.
If you knew that.
Bonus points for you.
Ding ding.
And the essay will say molecular fusion happens when a daddy molecule and a mommy molecule love each
other very much.
It's not really how it works but that's just my my my thought on how it works anyway.
So we have four question types for answer types.
They have explicit values and we can initialise a question K of type question.
So pretty cool.
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
it's going to basically be a blueprint for our question generator.
Let's say we're building a quiz app.
And of course this data would probably come in from an API pulling random questions but we need to basically
set up a blueprint that's going to allow us to generate questions when we need to.
So go ahead and type protocol question generator and inside our protocol we're going to declare a method.
OK so go ahead and type phunk generate random question and we're going to actually have this return
a question to us but remember we don't have any kind of implementation inside this protocol so we don't
need to put curly brackets we don't need to write any code.
The protocol is just the blueprint.
So basically this this helps us to include what we actually need and to include the kinds of functions
that are required for a certain feature.
So if we conform to the question generator protocol maybe in a quiz class later on.
It basically reminds us hey you need to be able to generate questions for this quiz and it's just a
way to kind of compartmentalize and put features in a certain place where they need to go.
So now that we have our protocol here and we have the function generate random question that returns
a question we can go ahead and create a class for our quiz case.
So go ahead and type class quiz and we're going to conform to the quiz generator
protocol like so.
OK.
But it's going to throw an error at us it's going to say hey we don't have that type.
And that's because.
Oh no wonder that's because it's a question generator.
OK.
There we go.
Question generator and it's going to throw an error at me saying that we have not yet conformed to question
generator.
That's because we are missing our blueprint instructions.
Generate random question.
So inside of this class I'm going to go ahead and I'm going to call generate random question and you'll
notice the error goes away because we have added all the functions that question generator says we need.
We have not yet done anything with them but we've at least called the function.
So that's cool but now we need to be able to generate a random in here.
So let's go ahead and let's think.
I want to basically choose a random question type and then I'm going to basically pass in the values
needed to generate a question which is a type a query and an answer.
So let's go ahead and let's generate a random number first.
OK so let's just say let Rand numeral equals and we're going to use arke for random uniform.
And we're going to set an upper bounds of 4.
OK.
Now that's going to generate numbers between 0 1 2 and 3 K for values which is perfect when dealing
with the index of something.
So that's really cool.
The issue though is that we're actually going to need this to be of type int and not type you and if
you look you'll notice that arc for random uniform returns a 32.
So let's go ahead and type in it and we're going to pass our arc for random uniform function into that
so that it is of type integer.
OK so let's say this generates a number 2.
Now in our incomes we need to be able to allow number which remember if we're dealing with an index
number two is really the third result.
So that would be short answer but an enum in and of itself does not have any kind of indexing system
or a way to identify first second third fourth.
So what we're going to actually do is inside of our Inam we're going to create an array and we're going
to basically just let We're going to pass in all of our types so that we can order them in some type
of way.
So we're going to go ahead and just type let types and we're going to go ahead and pass in true false
WIPs true false multiple choice.
Short answer and essay.
OK.
And this this array is going to allow us to parse through and I can pull out the index here at index
1 and I can pull out multiple choice K that's going to be how we identify the different types that we
want.
But we're getting an error here.
Check it out.
Anime's may not contain stored properties in order to circumvent this we can create this as a static
constant.
OK.
Now a static constant is basically going to allow this to exist for the entire life cycle of the app
since it's static.
And we're going to use that because we need to be able to access it all the time throughout this when
this code runs.
Now this is great for questions but we're also going to need it for our answers as well so I copied
it and I'm pasting it down below.
So we have a random number.
Now we're going to need to use this number to basically pull out a random type from our Random type's
array inside of our enumeration.
So go ahead and type let random type equals question type and what we're going to do is we're going
to pull out our type's array and we're going to pass in random Numeral.
Now let's say that was two or three whatever it is it's going to pull out a random one.
Now as it runs you'll see but we're getting an error here.
And it's saying that we have not yet returned a question.
That's because we have not yet returned the question so let's keep building it.
Remember a question needs a type a query and an answer.
So let's go ahead and type let a random query equals random type and we can pull out the raw value.
Remember if I pulled out multiple choice the raw value is going to be the string here.
That's my question.
OK.
Easy enough.
So there we go.
Let random answer equals.
And where are we going to get the answer.
We're going to get it from our answer type in numeration and we can pull out the specific type using
our array.
Now if I pulled out multiple choice here I can use that same numeral value here to pull out the multiple
choice answer.
So let's go ahead and let's call.
Answer type types and I can pull out the right value using random numeral k that will get the proper
answer back for me.
So now that we have those three values we can create a question.
So Type let.
Random question equals a question and we're going to instantiate it here with our random type random
queery and random answer.
OK.
Now at the very end we need to go ahead and just return our Random question.
That's not a question of random question.
There we go.
Supercool so as it goes through the function will properly generate the value we need.
But I'm noticing that it is giving us an issue here.
It is saying we cannot convert answer type to expected argument string.
So random answer is coming in.
But we are just getting a random type here we are not pulling out the raw value.
Oh that's right.
So instead of that at the very end here I can go ahead and pull out the raw value for the item we pull
out of that index.
And now that error should go away.
Really cool.
So that's great.
Now we have a quiz we have conformed to the question generator protocol and we have put in all the implementation
code to generate a random question from our enumerations.
So now if I create a quiz check it out I can type.
Let quiz equals quiz.
And then I can call quiz dot.
Generate random question and watch what happens when I do that.
It runs and it pulls out a question.
Supercool.
Now it pulled out a question type of essay and it pulled out an answer from our answers array.
Now we have a type a query and an answer.
That's so cool.
So let's go ahead and let's actually print out what's happening here and actually what I'm going to
do is I'm going to put this inside of a constant like so so that I can actually pull this out.
So let's just say question type whip's type and I can pull out.
Question.
Question dot type K and you'll see it will print question type multiple choice.
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
do forward slash and to give us some space here.
And for the queery we're going to pull out.
Question.
Queery and then slash and space and answer we can pull out.
Question Answer.
That's not an answer question that answer.
So there we go as it prints we should get everything cool.
Question type short answer.
Queery what is the capital of Oregon.
Answer.
Salem everything shows up exactly as it should.
This is how powerful protocols are.
It gives us the ability to basically create requirements that must be conform to when using that protocol.
And the cool thing is we can do a bunch of different things like lets say I wanted to create a new kind
of protocol maybe one that generates answers and then you would have to do the questions kind of like
Jeopardy.
So lets just go ahead and do protocol answer generator and a protocol can actually inherit or conform
to another kind of protocol.
So that's really really cool.
Now if you wanted to you could use this as a way to add additional functionality so phunk generate random
answer.
That could be my question and I could create a an answer type here.
You know what just for time let's just go ahead and just create a class here of type answer just so
that it doesn't yell at me.
And if I wanted to I could create a class called jeopardy.
And I can conform to my answer generator.
OK which is really cool but you'll see it's going to yell at me.
It's going to say hey you have not yet conformed to answer generator.
Now I can go ahead and type generate random question or generate random answer because I'm conforming
to that protocol through my other protocol.
So you can do it just like you can subclass classes.
You can sort of subclass protocols as well which is really neat.
So let's go ahead and let's conform to generate random question.
So let's go ahead and let's conform to generate random answer and you'll see we're still not getting
conformance.
And why is that.
Because we're not generating a random question as well.
In order to conform we have to meet the requirements of both to generate random answer and generate
random question.
K that's just something we need to do.
That's part of the power of protocols you can create a bunch of nested protocols just like that's pretty
cool.
Now of course we're not returning anything here so it's going to yell at us but I hope you can see how
helpful protocols can be and at least some small ways.
And in the next couple of videos we're going to build an app that allows you to pass values back and
forth between View controllers using protocols as a way to delegate information.
It's called the delegate method and it's something that's commonly talked about with protocols we can
use protocols to delegate information to other classes because remember a protocol itself does not contain
any implementation we implemented on a controller.
And so using conformance between two to view controllers we can use protocols as a way to delegate that
information.
It's really really cool.
So let's go ahead and let's head over to the next video and let's build it out.