Everybody what's going on this is Caleb with devah slopes dot com.
And in this video we're going to start talking about protocols and delegates.
OK.
Protocols are really really amazing feature of swift particularly but of couple other programming languages.
And basically what a protocol is is.
Think of it like a blueprint.
OK.
Now this blueprint can contain methods or functions properties and other requirements that fit a particular
task or like a piece of functionality.
And what we're able to do is we're able to conform to a certain protocol from a class form from a structure
or even an enumeration which you'll see later in this video.
And that enumeration or that structure or that class can provide the actual implementation of the methods
or properties or whatever you set in your protocol.
So really really cool.
Let's dive into it now.
You're going to want to open up X code beta and we're going to get started with a playground.
We pull it open in the right window.
Here we go and just click blank and I'm going to actually let's put this in the proper folder for protocols.
Call this protocol mania.
That sounds fun.
Press create.
And here we go.
OK let me bump up the font size a bit and let's get rid of this.
We do need you like it.
We don't need that string.
And great.
OK let's let's get after it.
So let me start by showing you the protocol syntax which you can use by simply typing protocol and then
giving a name to your protocol.
Now it's the same thing that you would do for naming a class or an enumeration or a structure.
All capital first letters of all of the words so if there's three words there's three capital letters
for this one we're just going to call it number and k.
And we're going to be doing some fun things with different types of numbers and I'm just going to show
you how protocols can be valuable.
Then we're going to do another example using some classes and a couple of other things to show you how
it can be used in class instances and then in the next couple of videos we're going to use protocols
as a means of delegating information between View controllers and lots of really cool stuff.
So let's just begin by getting familiar with protocols in general.
So inside of a protocol you can include methods and you can also include properties.
So I'm going to include a property in this protocol called a float value and I'm going to declare it
by typing var float value of type float.
And if we want this property to be accessible we can go ahead and give it some curly braces and then
inside these curly braces we can choose to put get or get set.
And that will allow us to either just get the value of the flow value or be able to set it as well.
But for this video we're only going to need to get the value and I'll show you why later.
Now let's say that we wanted to include a function as well maybe get flow value ok.
You just declare it like this func get float value and you include the parentheses for showing that
it's a function but you're not going to put the curly braces and watch what happens when I do that protocol
methods may not have bodies.
Remember the description I said at the beginning of the video the protocol is not where you declare
the implementation.
OK wherever you conform to this protocol later on that's where you'll declare the implementation and
I'll show you what I mean.
But for now just get rid of this function and leave the variable float value.
So what we can do with this protocol now is we can conform to it just like you would with subclassing.
So if I wanted to create a square class and then create maybe a rectangle subclass that inherited from
square I could do that by typing class square whip's where and then.
Sorry I meant class Rectangle and I could inherit from class Square.
That's one way I could do it.
So for protocol's it's kind of the same thing.
I'm going to make an extension of float.
OK.
Now if you've already seen the extension video that's great so you'll have some familiarity with extensions.
I'm going to extend the float type and I'm going to conform to my protocol number.
Now you'll notice there's going to be an issue that says type float does not conform to protocol number.
Now remember I said that a protocol is sort of like a blueprint.
Right.
A set of instructions in order to set something up the right way.
So what we're saying is if we conform to the protocol number we need to include the variable float value
what we can do to do that is we can basically call float value and you'll notice that it shows up right
there and my list of properties if I press Enter you'll see that it passes it in.
But what we need to do is we need to return a certain value to this variable so we can do that by typing
return.
And let's think about what do we need to return an extension of float.
We need to return whatever value is passed in as a float.
So let's say I create a variable of type float and I should return self to float value.
Right because it already is of type float.
So that's great and all but that basically is going to give us the ability to pull out the float value
of a certain constant or variable.
So like for instance I could do this let 4 of type float equals four.
And now if I wanted to get the explicit float value I could type for.
And as you can see we can pull out the float value in case we've extended float to include the property
float value.
Pretty cool.
So now let's think if I wanted to do some math maybe between of our three of type int equals 3 and var
4 of type float equals 4 if I wanted to do three plus four.
I'm going to get an error.
And the reason for that is because the types are not compatible int and float are not the same so we
can't add them because it doesn't know how to do that.
But what we can do is we can write an extension of all of our number types and we can give it the float
value property that's going to essentially convert it into a float.
Let's do that.
So let's go ahead and type extension and let's extend double because that's one of our number types.
So we're going to conform to the protocol number and then to officially conform we need to include all
the properties.
So let's go ahead and give it the value for float value.
And now we need to think what are we going to return this time.
We need to return but we need to think if a double is coming in.
We need to somehow convert that into a float and how we can do that is by doing typecasting.
So go ahead and type float and we're going to give it self meaning whatever double value we have.
We can pass that in itself and we can encapsulate it as a float and return that to to our variable.
So instead of doing three let's let's change that to a double.
It's still not going to work because they're not related types.
But what I can do now is I can type three float value and I can add that to for now type compatible.
So if I actually were to do this it would print out three plus four equals seven successfully and it
would be of type float because using my extension I can now pull out the specific float value for that
variables.
Pretty awesome.
Let's go ahead and let's get rid of that and let's keep extending this extension of not of type number
but we're conforming to the number protocol.
Let's go ahead and call float value and let's return just like before.
Float.
We're going to encapsulate self and cast it as a float.
Next we're going to go ahead and call extension you and because there are many times where we will have
you int values go ahead and conform to the number protocol call float value and return float self.
Self encapsulated as a float.
So now we have four extensions we can access the float value of any of these.
Now which is really cool.
And now let's do our best to write an equation that's going to let us basically add numbers of unrelated
types.
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
sure that it's a double by adding some decimals at the end.
Let's create another value called Y of type int and let's go ahead make that equal to 5 because it's
an integer it must be whole value.
And let's go ahead and let.
Q I'm going to divide these.
Let Q be the quotient and it's going to be equal to x divided by Y.
OK let's see what happens.
It looks like we're getting an error here.
Check it out.
It's saying
the binary operator divide cannot be applied to operands of type double and int k so we're getting the
same problem as before.
But what we're going to do is we're going to write some functions here that will allow us to make these
work nicely.
Now one thing we could do is we could do X float value divided by wide float value.
But what if we don't think to pull that out all the time.
That might not work.
So as you can see it does work to do this.
But what if we just wanted to be able to divide unrelated types.
We can write some functions that are going to allow us to override how these binary operators work.
Let's do that really quick it's pretty cool.
So go ahead and we're going to basically just write phunk add K and inside of add we're going to pass
in two values right value a of type number k and value B of type number and we're using our protocol
there is kind of like a class to pass in.
Now inside this function if we're adding we're going to take value a whatever we pass in float value
and we're gonna go ahead and add it to value be float value.
The only issue is that we actually need to return a value here K and we're going to return a value of
type float.
Let's say we're just working with floats so we can return it like so return value a plus float value
plus value B plus foot dot float value.
So many so many floats there.
OK.
So that is how we can do the addition function.
But what we're going to do is we're going to do this for addition subtraction division and multiplication.
So copy this function just for time we're going to paste it a few times.
I wouldn't normally do this but just for time time's sake we're going to replace plus with minus and
do that again replace plus here with Divide and replace it there as well and then replace this with
an asterisk for multiplication and do the same thing there.
Now we have these functions whenever we call Plus it's going to use it like a function.
We're going to take value a.
Pass it in value B pass it in.
Add them together and return the value.
So now watch what happens.
I have two unrelated types double and end.
But when I use the division operand check out what happens it passes in these two values and it returns
a float.
It works.
I am able to do operations on two unrelated types thanks to protocol's super super cool.
So as you can see protocols are pretty awesome they allow us to basically set a blueprint for values
that we are going to use or we'll need to use.
And this is just one way they can be used.
I think I'm actually going to split this video into two.
And in the next video we're going to actually see how protocols can benefit us when using classes or
structures et cetera et cetera.
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.