Hey what's up.
Jack Davis here with Defoe's dot com.
And tonight we're here to talk about a very popular subject over the last year or so and that subject
is protocol oriented programming.
There was a talk given w DC in 2015 that generated a huge amount of buzz on this subject.
It continues to generate buzz to this day sometimes is referred to as the crusty talk.
I highly recommend that you watch this session if you haven't already.
What edition of protocol extensions protocol oriented programming has become even bigger.
And in this introduction we're going to cover a few different topics such as swift tops the differences
between object oriented programming and protocol oriented programming protocols themselves protocol
extensions and generics.
Then to wrap things up we'll take a brief look at practical real world examples of how protocol oriented
programming can be used in your own projects.
Now this is just an introduction and this is a topic that can get very complex.
I hope you're as interested as I am and digging in continue learning new ways that protocol oriented
programming can help you to write better code.
There is a ton of information out there about it.
With that said let's move on and get started.
First we'll quickly cover the swift type system.
So for the first part of this segment we're going to talk about swift types and I'm going to pull up
a whiteboard at appears so I can sketch those out for you.
This is just going to be a quick run through.
Hopefully you're already familiar with them.
Some of the things you may not be familiar with but we're going to go ahead and lay those out just because
they may come up later on.
So let's pull up a white board here.
All right.
So we've got swift tops
and basically swift taps are broken into two categories.
We have nine tops
and we have compound tops and we're not going to talk about compound tops for a whole lot but I'm going
to go ahead and lay them out here just so you know how it works.
Compound types.
We have a group of top set or functions.
And the reason I say it's a group of tops is because it includes functions themselves
as well as closures.
And then also in compound types we have tuples
and I don't know if you're familiar with tuples or not you may have seen them in code before.
It's basically where you have two two or more types enclosed in parentheses.
You can pass those around in your code if you don't know what tuples are highly recommended that you
looked at.
Those are very handy in certain instances.
So let's look at nine types and that's mainly what we're going to focus on here and under nine types
we have we have primitives
and primitives are things like integers doubles strings characters things like that in most languages.
Those are all just built in to the language in swift they're handled a little bit differently they're
actually implemented in the standard library structures.
Most of them anyway and will will probably take a look at that a little bit later.
I just want you to understand that in swift the way that they create those is they take structures and
they put extensions in there to give them functionality that's built into other languages.
So we've got primitives.
So we've got so primitive as a name type but it's also a value tot and undervalue types besides primitives
we also have structures
and gnomes or enumerations.
All right.
And then also under the name types we have reference types
and it's kind of important to understand the difference because if you don't it could cause problems
and under reference types of classes.
And with this being a little bit more of an advanced topic hopefully you are familiar with Object-Oriented
Programming and classes and a lot of times we just kind of default to using classes.
But we're going to talk about object oriented programming obviously.
And the fact that sometimes classes aren't always the best answer.
Sometimes it's better to use a structure or it as a value type.
And basically what that means is value types are passed as copies.
So when you say the eraser.
So let's go ahead and clean this off here.
So we're going to talk about the way objects are created.
So if we have a reference type if you remember that would be a class
and we've got a value type over here.
And for this case we're just going to say a structure.
So over here we've got a class.
And over here we've got a struct.
Now if you create an object out of each of these things so say we do a variable and we'll say car one
equal to an instance of a car
OK.
And then over here say we implemented car as a struct instead of a class so we can save or we'll save
our car one equals car again.
Now obviously on the left side we've implemented a car as a class on the ride as a struct and then if
we come in and we save our car two equals car 1 and we do the same thing on the other side
we have two different things here on the class side or the reference side.
Both of these instances point to the same object.
So if we did something like car to dark color and the default color was blue if we do something like
car to color and we say equals red since that's a reference and they both point to the same thing car
one
color is also going to be red.
Now the end on the stroke side which is of value it's passed by value.
So when you make this assignment in this line right here what that does it doesn't copy a link or reference
to a car too it makes a copy of the actual data and passes it as a copy.
So if we do the same thing over here
when we say car to talk color equals red car one color is still going to be equal to blue.
And it's kind of an important thing to understand.
In most cases we keep up with the flow in our programs and we know exactly what's going on.
And if you get in a very complex hierarchy and situation you've really got to be aware that if you set
something in one part of your code that you don't have unintended consequences and another part of your
code.
So that's kind of the point of this here's just demonstrating that classes are passed by reference.
So if you create if you set something to a copy it's not a copy it's a reference they both point to
the same thing whereas a value type.
It passes it as a copy and there's places where reference types are better suited for the task.
And there's other places where value types are suited for better suited for the task.
Apple always recommends going with a value type wherever possible to quote parts of the WWT see 20:15
video I spoke of earlier.
Strokes are preferable if they're relatively small and copyable copying is way safer than having multiple
references to the same instance.
Especially important when passing around a variable to many classes in a multi-threaded environment.
If you can always send a copy of your variable to other places you never have to worry about that other
place changing the value of your variable underneath the strokes there is no need to worry about memory
leaks or multiple threads Reisen to access a single instance of a variable.
Classes can also become bloated because of class can only inherit from a single superclass.
That encourages us to create huge superclasses with many different abilities that are only loosely related.
Use protocols and protocol extensions allows us to eliminate the need for classes to achieve that sort
of behavior.
That's the official word from Apple on the subject.
Struction gnomes are preferred and classes should be kind of like a fallback whenever they make sense.
There are some instances where classes are going to be the only option you have.
Anyway I hope you enjoy this intro and I'll look forward to see you later.
Later.