So will come back again in this lesson and we going to talk about class extensions and what a class
extension is is essentially exactly the same thing as a category you know Objective C but for swift.
So if you use classic stations to add new features to existing classes or the classes that we don't
have access to all the classic stations to add new features to our own classes simply for the purpose
of marginalizing our code.
So in this season I'm going to show you how to use glass extensions which is going to be rather simple
and then I'm going to show you a picture essentially of an app that I'm developing myself and you can
see how widely I'm using extensions just so that you know I make sure that my code is simple and readable
and my colleagues who are working with me on the app or so easily understand my process.
So go ahead and do it.
And I'm going to start a new project this time a command line for swift and I'm going to call it class
extensions.
And then in here cause extensions are very very simple.
Here is what we do.
I'm going to go ahead and say I have a class on that class I'm going to call it my class doesn't have
a superclass or does have superclass.
It doesn't not function do something.
And that's again very simple something you have seen many many times.
It has a function that does something and then I'm going to go ahead and say extension and this extension
is for my class on my extension has a function let's call it.
Do I not think so.
That's about that.
I have a clause on that clause has an extension and if I go ahead and say let's use this by saying that
my object is my class.
So I made an object of that class and I can simply go ahead and say my object d'art.
Now we have access to both of these functions the function from the actual cause of the function from
the extension class and that's about that that's how you do extensions.
You're right extension the name of your class and then you are.
It is as if you're inside the body of that class and you are writing your stuff whichever way you like
it to be.
One thing that you have to keep in mind is that you can do something like this for instance the channel
say I have a string between the body of the extension.
These kind of things the stored properties you must have them in the original class dictionary.
So you have to actually do it here.
Primarily we use or at least I use class extensions for situations when I want to do functions if I
want to add extra functions there and particularly for when I want to do something called a class function.
That's something I want to show you in the next lesson up.
This error obviously you know what's happening is that this object isn't explicitly arms wrapped optional.
I'm saying well you need an initialization.
I'm going to say if this was the case just to silence that error I'm going to say you with a default
value.
So that is extension.
Now let's go ahead then use extensions on an existing class something that is needed.
Right.
So I'm going to go and I don't say remove all of these say extension strings.
So I want to add a new feature or a new function with string class.
And the thing I want to add is this.
What we did before which was numbers only.
So it's going to be much easier put in the lesson that we delta-V textfield delegates here and in here
I had this function and then copy all of these.
Of course we have to change some of it but there's a matter I'm going to bring it to my new extension
on say that is a function and this function is called numbers only.
And what is it going to do what is going to return as Thring which is only the numbers of that that
the input value.
So I'm going to go in here say that numbers is that remove characters that is purrfect separated them
terrified.
Let's join.
Blah blah that's perfect.
So what I want to do is this.
And the ending here.
I want to say to return what do I want in return.
I want return the joined version.
So I want to get the characters in something and then that thing I want to make sure that they return
the one that only has numbers.
Now what is that thing that they want to get from that is itself.
Because self is this a string.
So I'm going to get this stuff from self bracket's the sky.
There's set everything that is not and numbers and then add them to each other and then return that
one.
So now I have this function let me get rid the all the comments it's easy and I can you know zoom into
the whole thing easier on that I'm gonna go ahead and say I have variable mightest are which is the
type of string and it is actually this that I've got.
There's a lot of numbers of random characters.
I'm going to save prints numbers only.
Is what is that that is going to be.
Boy I need a semicolon.
I'm typing back and forth between Objective-C and sweet and this is going to be my s are I've got.
What is it on numbers only.
So I can actually go ahead and say hey get my string reaches a string and apply a function called numbers
only.
And this function only works here because I have the extension for a string in here.
You fit on this.
Now we should be able to see exactly what we expected to see
say numbers only is these parts are we shouldn't have any way of making short of it both is five for
eight Yes nine eight and for six.
And then at the end it got to 500 on 60.
Perfect.
So that is how you got extensions.
Extensions are very very common.
Like I promised I'm going to show you one app of mine.
Here's the OP you're kind of halfway through.
As you can imagine that is so much is still happening.
And we haven't finished a lot of the different things in here.
But as you can see every class is broken into so many many extensions simply because I want my code
to be easier and easy easily readable for my colleagues on you know anyone else who might be accessing
these.
So for instance if I go through a cloud that reads single Whatever is all the code that there is in
this one fine.
This is all the code that is in here.
It makes things a lot easier for me.
Let me show you another one that kind of you know perhaps makes easier sense and a simpler one here
for instance I have a property view controller that is you know a view controller like the ones we have
seen in the past.
And then my property viewpoint you to has something that at some borders it has something that adds
that content to.
It has something to do with the map.
It has something to do with the labels in it.
But each of them has its own extension simply because by having the extensions I make sure that my code
is far more modularized and it's also a lot easier for everybody else to read it.
If I tell a colleague of mine that there seems to be an issue with the map in the proper The View Controller
they know they have to immediately coming here and they work on this one file which really makes our
lives a lot easier.
Specifically when we were working together on larger projects we did see these in the future as well
when we work on version control on how you have to you know essentially go with the best the strategies
of making sure that you and other people can work easily with each other.
So with that in mind let's move on to our next lesson.
This was extensions and I'll see you in the next line.