Hey Jack is here with themselves to welcome you back for part two of our journey into protocol oriented
programming.
In the last lesson we talked about the difference between reference and value types or classes versus
structures.
One of the limitations that you tap taps have had in the past is that I do not offer inheritance with
classes in swift.
We have single inheritance.
This means that we can implement functionality in a superclass and then use that functionality in our
subclasses.
Some languages offer multiple inherit inheritance where classes can have more than one superclass but
in swift we're limited to exactly one superclass the inheritance problem with value types has virtually
been eliminated.
With the advent of protocols and protocol extensions protocols are kind of like contracts or blueprints.
They define what functionality a class or structure are all for but they do not provide any implementation
code.
They simply define requirements for types that conform to that protocol.
Some other languages offer a similar feature namely interfaces with protocol extensions you can now
extend those protocols and any types that conform to them and actually provide common functionality.
This functionality or implementation code can be functions or computed properties.
Note here that properties must be computed values cannot be assigned directly to properties of protocol
extensions.
In this lesson we're going to go through a playground project and take a look at protocols and the next
list will add and protocol extensions and finally we'll briefly cover generics before we move on to
some real world examples of ways to incorporate protocol into programming into your everyday code.
So I was going to pull up a new playground here.
To get started and we'll just call this protocol's
and we'll leave it is our way saying we'll save it on the desktop
or get rid of that line there.
Now the first thing we're going to do is we'll go ahead and create a protocol for a vehicle.
So in this instance we could do if we were modeling using reference types or classes we could have a
base class that was a vehicle and we could extend that to subclasses in the form of more specialized
classes.
In this case this is all about protocol so that's what we're going to do here.
So.
So define a protocol of vehicle and we're going to have a property here for it and we'll just call it
is running and we'll make that a boolean and in protocol's you define whether it's Read-Only property
or reroute property.
In this particular protocol's going to be a read write property so we've got to specify get set there.
All right.
Now we're going to have a couple of functions in here and since we might be using this with a structure
there's one little caveat here.
When you define a function in a protocol so we're going to say thanks start.
And this is just going to be a function.
Again these are just blueprints or contracts and whatever conforms to it has to implement these functions
in their body.
So we're going to have a start function and we're also going to have a turn or function.
The caveat here is if you use these with value types you have to specify
mutating in front of the function.
If it's going to mutate a property inside the structure or a value top that is going to conform to this
so we'll go ahead and add that in here.
All you need to know is if you're going to use structures with this and you're going to have functions
that modify internal properties and you have to put that mutating keyword in there.
So this is just Luxa.
This is a real simple example but we've got our vehicle protocol here and everyone go ahead and create
a sports car that is going to conform to the vehicle protocol and the sports car is going to be a struct
so we'll use a value top here.
All right.
And then now we've got data since it conforms to vehicle you'll see here the error it says top sports
car does not conform to a protocol vehicle.
Well that's because we haven't implemented any of those required properties or method yet.
So now we need to go in here in
specify our properties so we're going to set this one the false by default.
And now we've got to do our functions.
So this being a structure we need the mutating keyword and you'll notice there that it can.
Since we're conforming to that topic kind of feel some of that for you.
So in the Star function we're just going to say it is running.
So if it's already running we're just going to print it
already started.
Otherwise we're going to say is running equals true.
In the end we're going to present
room.
Think that's kind of how sports car sales are.
12 Hardeen.
All right now we've got another function here that we have to implement and this one is all.
So we're going to say it is running and we're going to say is running equals false and we're going to
preempt crickets.
I have no idea.
It's just.
Otherwise we're going to say prent.
I already did.
OK.
So we've got our is running property here.
We've got our start function in our turn off function.
And you'll notice here that we've lost our error.
So now we conform to the vehicle protocol now that we've implemented all of those properties and methods
and protocols.
Any any type can conform to a protocol so it doesn't have to be a value type it can be a reference type.
So just to prove this we'll go ahead Mike our next one a class so we'll say a semi truck and we'll conform
to the vehicle protocol.
And again you'll see here we get our error.
It does not conform.
So now we need to add in our property and we'll say false to begin with we make a little bit more space
here.
All right.
Now this is a class so the mutating keyword is not required here.
Made a reference tot the properties in here are immutable.
We don't need the mutating key word and it doesn't hurt that it's seeing that it's in the protocol declaration
that is just in case you use it in a structure then you're able to modify your properties.
So in the class we're just going to say phunk start and we're going to say if it is running roadside
print
already started.
Otherwise we're going to say is running is true and we're going to present.
I don't know how does an 18 wheeler sound rumble.
Maybe that's a good word for it.
All right.
So we've got to turn our star functionality or turn off function
so we'll say if it is running and we're going to kill it here so we'll say is running equals false hopes
and we'll say Brant
I don't know.
Silence.
And then if it's not running we'll say
already shut down I guess.
All right.
So hopefully you need those if else is backwards.
But OK now the end.
You can also just because you conform to that vehicle protocol you can still add other functionality
in here.
So let's say in the case of the 18 wheeler we want to add another function
called blow air horn.
If I can type and we're just going to preach it how does an air horn sound.
What is a tune.
All right.
So now we've got our semi truck class which conforms to vehicle and we've got our sports car struck
that conforms to vehicle.
So let's move on down.
Let's go and create some instances here.
So we'll say car one is equal to a sports car and say truck one is a call to a semi truck.
All right.
Now we can do car want to start.
We can do truck one
star and we can say truck one blow and we pull up our console down here.
You can see that we're putting these things out.
So we've successfully conformed to that protocol.
And the real power with protocols coming in with protocol extensions are a lot of the power.
And we'll go over that in the next lesson.
But one really cool thing about this let's go ahead and turn these off.
So we've got them both shut down.
One really cool thing about this is we can do something like we can make a vehicle array and we'll just
instead of using the brackets syntax we'll use this the bracket syntax is actually an alternative syntax.
This right here is actually house defined in the standard library.
And for the tab we're going to make it a vehicle top so we're going to put in here car one and truck
one.
So now we've got an array even though these are two different types.
One is a sports car and one's of Sumatra.
They're in the same right because they both conform to vehicle.
So you can use protocols as tops.
This in this situation.
And then if we go ahead and walk through them
we'll just print out the vehicle.
So now you can see down here we've got a sports car and it's got the is running is false and we just
got a semi truck.
So they're not there's no standard description here that's printed out.
What kind of going to that here in just a second.
But if I try to do something like vehicle dot
blow air horn I get an error here because the top vehicle has no memory blower horn.
So basically we could use any of these things that we define our protocol but we extended our Sumatra
and added this blower horn function.
Right.
So this being a top vehicle it knows nothing about blower horn.
If we wanted to use this in this array as we walk it.
So if we did something like gef let the cool cool be cool as a semi truck then we can say we call it
blow air horn.
Now you can see down here in the car.
So we get to blow air horn.
So basically we look through this right car was not a semi truck so it skipped over the next element
was truck and it blew it blew the air horn.
So that's pretty cool.
In addition to that protocols can be combined.
So there is a protocol called custom screen convertible that a lot of types conform to.
And I'm going to editing it here.
Click on it and we'll go look at the implementation.
So you say here this is a protocol custom inconvertible what's in the standard library and it gives
a textual representation of the instance.
And basically when you conform to this you have to add this property here description and it's just
a good property.
And so whenever you call the description on your object you give it a friendly name to print out or
to return.
So let's go back and let's implement this.
So this protocol is in the standard library so we're just going to add it to our vehicle protocol here
in the way you do that is sort of how you either conform to the protocol or you inherit from a superclass.
So you just put your colon here and then we add custom string convertible.
And now we're getting in the air here because it does not conform to it.
So now we need to add
description.
So we need to add description into our sports car and this is we're going to return
we'll see
yeah let's do it this way.
So we'll say if it is running we'll present sports car
currently running.
Otherwise
we'll present.
And then we'll do the same thing down here in Sumatra.
And you say we don't have to specify because some steering convertible here because we specified it
in our vehicle protocol.
Now we could take this out and add it.
Let's see we implemented it here.
So so now we're getting this error on this description property here we could just put a comma in the
custom string convertible.
Sorry we don't need a print here.
Wanting a return
are sort of taken care of.
All right.
So their goals are.
So now the
now that we've got that now we've got it in the sports car that functionality that we don't have in
Sumatra.
And then again if we wanted to make it available to every vehicle we just go ahead and put it back in
our protocol peer when we do that we see right here that need implemented in our in our semi truck as
well.
So if it is running and we need to return not pregnant or semi truck currently running otherwise it
turns Sumatra currently shut down.
OK.
And that takes care of that.
So now the when we loop through this we'll take this code out of here.
We'll just come back we'll look through and we'll print the vehicle description because it's tied in
with both of them now so we'll say
print
the vehicle description.
And so now down here in the car so you can see we've got a sports car currently turned off in a semi
truck currently shut down.
So now if we come up here we've got them both turned off if we just take these out.
They should change it.
Now you can say our sports car is currently running in semi truck is currently running so we could just
shut one of them down.
So we could turn off truck one.
Now you can see our semi trucks currently shut down.
So that kind of covers protocols in how you can form to them you still have to provide all the implementation
code.
And like I said that's where the magic happens is when you start doing protocol extensions you can actually
provide some of that implementation.
And I'll talk about why the next lesson.
But that's our introduction to protocols and we'll continue to put it together in the next lesson.
Thanks for watching.
We'll see you next time.
Ligers.