Hey everybody has gone this is Caleb with slopestyle.
And in this video we're going to be talking about extensions in swift now extensions are amazing because
they allow you to add functionality to an existing class structure an enumeration or even a protocol.
OK.
There's lots of different things we can do to extend the capabilities of certain classes either or classes
that have already been created by us or even classes we create ourselves.
Now this video is going to be an introduction.
We're going to be using a playground and I want to show you a few cool things you can do with extensions.
And then in the next video we're actually going to build a very very simple app to show you how extensions
could be useful to you as a developer.
So let's go ahead and let's pull open X code and let's go ahead and get started with the new playground.
So click that button and of course it's going to pop open on the wrong window.
There we go.
All right click blank and let's call this extension's fun time.
Yeah.
No just yet.
Let's do extension's Funtime gate click Create and we get ourselves a nice little playground to play
in.
So let's go ahead and let's do that.
So first of all I'm going to show you the syntax for an extension and to do that I'm going to get rid
of that and get rid of this silly comment at the top.
We don't need.
And so let's begin to create an extension what we need to do is we need to type extension.
OK believe it or not.
And an extension like I said allows us to extend an existing class a structure a protocol or an enumeration.
So what we're going to do is to create an extension.
We're going to extend.
Think of like you're going to extend the certain class so let's think of a class and swift How about
integers k so extension int.
We're going to extend the int subclass.
OK.
And you set it up just like you would a class or a function you put a set of curly brackets and that's
where all of the important information goes inside.
So that's how an extension is made.
Now what if you had created your own class like class my class ok.
You can also extend your own custom classes and make your own extensions.
And inside is where all the functionality is going to go.
Whoops.
Functionality.
OK so we're going to add some functions and lots of cool stuff to extend the capabilities of a certain
class.
OK.
Now some of the things that extensions are able to do are there able to add computed properties and
computed type properties.
They're able to define instance and type methods.
They're actually able to provide new initializers So if you think about when you create something with
an initializer like a C.G. wrecked car you have lots of different ways to initialize it.
That's just something you can do with extensions.
Pretty cool.
You can also define and use subscripts as well as nested types which is pretty cool.
And you're also able to make an existing type conform to a certain protocol.
So lots of different things you can do with extensions.
And so now let's go ahead and let's take a look at just one way we could extend an existing swift class.
So let's go ahead let's make an extension of string type extension string.
And what we're going to do is we're going to build our own function that's going to be able to reverse
a string.
OK.
Now there already is one.
I know that.
But the interesting thing is I'll show you the difference later there is a difference.
And we're going to basically write a function that will return the exact opposite of a string that we
give it.
So inside of the string extension go ahead and type phunk reverse.
And you notice that pops up there.
Reverse there are the two functions that already exist within the string class reversed and it returns
an array of character and reversed and it returns a reversed collection of type string.
So those are the ones that already exist.
But we're going to write our own simply called reverse.
So what we're going to do is we're basically going to think of extension string here.
Think of this as something that can be applied to any type of string.
So inside of the extension let's say that we applied this function to a string we've created inside
the function we can just call it self ok because this extension is referring to whatever instance of
string this function is called upon.
So we're going to be referring to self to refer to the string that this extension is is connecting to.
So let's go ahead and let's write this function out.
Oh you know what we need to actually return a string at the end right because we need the reverse string
at the end.
So let's go ahead and create an array of characters.
So go ahead and type var character array and that's going to be equal to an array of character.
Like I said and let's just instantiate it for now.
Now we're going to do is we're basically going to parse through and for every character in the string
we've passed then we're going to go ahead and insert it into our character array at index 0 so that
every single time a new character is added it's added to the front and so therefore we will reverse
the entire string as we pass through all the characters.
So go ahead and type four character in.
Remember we're referring to self because that's the instance of the string self dot characters we're
going to parse through the array of characters that's already built into a string for character itself
dumb characters.
What we're going to do is we're going to go ahead and type character array and we're going to go ahead
and call insert a new element at it.
And that's like kind of like the index path of an array.
So we're going to pass it in.
Oh actually it is the index path of because that's what we're dealing with we're going to insert whatever
character we get through at index 0 so that every single new one is going to be passed in at zero and
it will bump all the rest of the letters down.
So the first character comes in and it's at index 0.
The next one comes in and now it's at index 0.
The next one comes and they all start to move down until the whole string is reversed.
At the very end what we're going to do is we're going to return the character but remember it's an array
of characters we need a string.
So what we can do.
Check this out.
We can call string.
And when we instantiate a string if you scroll down you can make a string from a sequence of characters.
So select characters and we can pass in the character array because that's a sequence of characters.
Pretty cool right.
So this is one function.
Now that is in our string extension.
I want to show you how to use it now though it's really really cool.
So what I'm going to do to show you this is I'm going to create a string.
Let's just do var name and I'm going to say Marty McFly.
OK and of course you can tell that that's a string.
Now what I can do is because the variable name is of type string.
I can call name dot reverse.
Now be careful don't use the top two but look.
Reverse without the D.
That's our function.
Anything that is a string we can call reverse on and check out what happens when we call it look at
the print out here we get a reversal of Marty McFly.
The string is reversed it's really really cool.
Now just to show you that this is only applicable to strings.
I'm going to create an integer like 12.
And if I try to call reverse on this integer it's not going to work because we only extended the string
class not the integer class so therefore it doesn't work.
That is one way you can extend string with a cool function that reverses any string that you attach
that function to really really cool.
Now we're going to go ahead and create an extension of it.
So let's do that down here below.
Let's give you some space here so you can see OK this looks good.
All right so go ahead and type extension in and just like we did with the string and we're going to
write a function that's going to basically allow us to square any value that we give it.
So go ahead and type phunk square and inside of this function we're going to just go ahead and type
self times self because that's what something squared is the value of it multiplied by that value.
Now of course in order for this to actually do something we need to return a value of int hopes of end
so that we can actually use this on an integer.
And of course since we need to return we'll just return self time self.
So there's our extension.
That's cool.
But let's use it what we can do is we can say var value equals nine.
And now what I can do just like with this string is I can call value dot square and watch what happens
when we print out in the terminal here.
You can see it prints out 81 it's squared our value and that's really cool.
But one of the most interesting things about extensions is that you're able to use what are called mutating
functions and a mutating function is basically a function that is going to actually modify the value
of the variable that it's called on.
So right now var value equals nine k value is equal to 9.
We call value Dotts Square.
And what it does is it takes the value of our variable here multiplies it by itself.
And this right here value Square is currently equal to 81.
But watch what happens if I print out value even below the function you'll notice it still comes back
as 9.
That's because we have not mutated the original value.
We're going to do that now with a mutating function and that's something really cool about extensions
is it's going to allow you to mutate a certain function K and to do that we're going to make a function
that's going to return the area of a circle.
OK.
So let's go ahead and let's extend Double K because we need some decimal places here because if you
know the function to determine the area of a circle is pi squared right.
We're going to take the radius of the circle square it and then multiply it by pi.
So go ahead and call extension double and using double again because we need numbers after the decimal
point.
And we're going to go ahead and write our area of function here.
So go ahead and type phunk area or calculate area.
That's a little more descriptive and let's create a constant to hold the value of pi.
So A-type let pi equals three point 1 4 1 5.
That's close enough.
You know it's not exact but it's pretty good.
And what we can do is we can say self is equal to pi times self time self k.
So we have our squared right the radius squared self time self and then we're multiplying that by pi.
Now we're getting an error.
And you can tell it says self is immutable.
And if you think back to our string extension we basically called name and then we called name that
reverse.
Now what we did is we took our name.
We printed it and then we took the value of the name and we reversed it.
Kind of making this like a temporary variable.
We did the same thing with value not square.
But what we want to do is we actually want to modify the value of the variable.
So to do that we just need to add a keyword at the beginning of this function mutating.
And if you look at the little description here of mutating I don't know if it'll let me.
It won't.
But basically a mutating function allows that function to modify the value of whatever variable it's
called upon.
So let's go ahead let's try this out I want to show you how it works and that to do that I'm actually
going to create a quick little class here for a circle that's going to have a radius property so just
really quickly type class circle.
Set a variable of radius and make that a double.
Then let's create an initializer with a property of radius of type double set self-doubt radius to be
equal to radius.
Sorry that was really quick but it's not super duper important.
I just want to make sure that you saw what I did there.
So we have our mutating function for calculate area.
We have a class to make a circle.
Let's make a circle.
So type var circle WIPs circle equals circle.
And we're going to give it a radius value so let's think about three point three.
OK so there's our radius three point three.
And what we're able to do is we can print out a circle dot radius and watch what we get printed out
3.3.
Just like you would expect right.
And let me pull this up here so you can see it.
There we go.
So we get three point three which is the radius we've passed then.
So if I want to calculate the area of my circle I can call Circle dot radius and watch what I can do.
I can call my mutating function here calculate area and you'll see in a second it'll pop up
go calculate area.
Cool.
So once this runs you'll see that it prints out the area of the circle thirty four point to 1 0 9 3
5 whatever it is.
Now you're probably thinking how is this different at all from what we just did.
Let me show you up here.
When we called a square on our value of nine what it did was it created a temporary variable holding
81 which is the square value for nine.
But if we printout value again you'll see that the original value of the variable value is still 9 K
like that it prints out.
Now what we can do though is with our mutating function we're actually able to change the value of the
variable upon which this is called.
So watch what happens if I print out circle dot radius.
Now check out what prints out thirty four point to 1 0 9 3 5.
That's the variable that we just changed.
We used our mutating function to allow us to modify the variable.
It's really cool and there are plenty of use cases for this.
But guys this is just kind of a brief introduction to extensions some of the things you can do now in
the next video.
I'm actually going to show you some cool things you can do in Interface Builder in Iowa with actual
things you might use in a real app case super duper cool.
Thanks for watching this video guys and let's head over to part two right now.