Sober come back again in this class and we're going to talk about a feature of programming for Iowas
which is called category's and answer specific to Objective C categories are a way for us to add the
functionalities and add new methods to existing classes.
Let's see how we could do that before I actually go to school them you know try to make an app that
explains category's.
I want to show you that you somehow have been seeing categories all along the way.
I'm going to take a nap.
There's going to be a temporary hour.
You don't have to make this it's going to be a single view application in Objective C and I'm going
to call it tamp up.
And if you remember whenever we make one of these applications you know objective Objective-C immediately
end up seeing something like this at interface view controller a known name in ASH and something in
the past I probably told you this is called a private category.
And that's exactly what it is.
It's a private category that helps us put private stuff in here things that we don't want our data h
to have it's interface to have it so it cannot be exposed to other classes.
As you can imagine any class that imports this one view controller will have access to whatever is here
which is in our data h.
So we use this one as some sort of a private place for declaring different stuff that we don't want
anyone else to essentially having access to them or we don't want them to be exposed outside.
So that's kind of how you declare and use a category.
Now let's go ahead and see how we will do it in a much simpler way.
I'm going to go to school it's say fine new project is going to be a command line tool in my class and
I'm going to call this one.
Categories are total categories tool.
The only thing it has to be objective c of course when added in here is what they want to do.
I want to go to my dad and my main fine.
I want to have a string.
Let's say that is this thing.
Hello world.
All right.
It is a.
It is called the hello as we are and that is this one.
I want to have a way to reverse it.
Essentially say r t s or l e h but I don't want to have a function here and I don't want to have it
as a separate class simply because I want the string to be able to do that very much like the way that
you could say hello.
Or a string by appending a string which is a function built into a string.
I want to make it like that simply because this is a function that I'm going to be using on and on.
Of course reversing it is not necessarily that case but you understand the meaning of it you could do
something that you can always use from that particular class.
So here's how we're going to do that.
I'm going to go up here say it's interface just like all of you have always been doing are declaring
classes but this interface is actually going to be a string type and it doesn't have a superclass.
It doesn't have a call of course but it does have two pounds.
Is that a stand for the name of the category.
And I'm going to call this category reversal.
It's going to be my reverse third category of any system.
And of course I have implemented its implementation.
What do you want to implement.
I want to implement this guy and then it does whatever it does and then we'll have to deal with that.
So the first thing I should do I should say I have a method in here that returns a string that's called
reverse.
So this method whenever we send something to it it has to reverse it.
So I'm going to say this thing is within the implementation as well.
And it has an end as a string let's call it the raise and return address.
Now obviously between these two we have to actually reverse it.
I'm not talking about that real soon.
But before I do that I want to put my interface and my implementation in separate files.
So I'm going to go ahead and say file mewl file my new file is going to be a cocotte class of that type
and it's a string because that's what we're trying to make.
And there's a very particular way for naming categories.
You don't have to be it's a very very common practice and that is called and that's a string and then
plus whatever it does for instance in this case it becomes an sustaining Plus the reverse.
This is minus things category that reverses.
And I think so if you find the file outside that somebody else has written and it has this particular
format it has a plus by then and then it has some sort of label.
It means that it does that thing.
So I'm going to say next I have it in here over there the house.
And then I'm going to copy this to.
So that's quite unnecessary.
Reverse the implicit interface of it somewhere pasted in here.
And then again I'm going to call this one implementation Olduvai.
I'm going to put my that in fact.
So that's about that.
This is my dad H and this is my dad.
And of course I can and my dad use it but we don't really know how it is going to work just yet.
So the next thing is write the body of here to do reverse smy a string.
So let's see how we going to do that.
Here is the logic behind it.
Let's say I have this very long set of characters I'm going to say first of all find out how many are
there.
I can count this but let's say 25 and then we're going to say well if they're 25 get this started from
24 which is that one.
Then add the 2010 which is that one and then add the 20th second which is that one so on and so forth
until you reach to the very beginning of it.
And that would be essentially the technique I'm going to be using to reverse a string.
So let's go ahead and implement that since I'm going to be changing this text.
I have to change this into.
And this mutable string.
It has to be an immutable string.
And then I'm going to go ahead and say without initialises of course and US mutable string new then
I have to find out the how how long is my time.
I'm going to go ahead and say it is an integer and that integer is called.
Let's say there are lengths for instance or a string length string length is going to be self simply
because V or of it in the end it's a string as a cell.
So how long is it.
This is for instance the one that returns 25 for us.
Now I could simply put this inside a 42.
I could go ahead and say.
Or better yet unaffordable for a situation like this is a while.
So I'm going to go ahead and say while the condition is this while this guy is still larger than zero.
All right.
So as we go through it Dad dad dad dad we go there as long as we're here at least we are zero.
You'd do this.
You say first of all subtract from yourself.
So it becomes a one less imagine indice 25.
We are Cranley landing.
I don't know somebody like 17.
So we are here on this.
So we have to go and say here right before W go for one candidate and that we do it with something called
arr..
So here is how we introduce a range and then I say.
A syringe and in my honest range I'm going to call it substring range.
That's a very good name usually substring range is and this may range and the range we want to make
is from here for one CACs.
So as you can imagine it says Major range from 17 12:56.
So only this one.
So what you want to do with that.
I want to find that.
There's something to say and it's a string of let's call it next block that actually makes more sense
to quite the previous character.
So previous character is going to be self wherever I am substring by range and this range.
So I'm going to say here if we wanted to find 17 and only 17.
Just one of them.
So now go to self find that place.
I'm putting something called previous sky that I'm going to say.
RAZ This one resolved append a string.
What do you want to append to it.
This one I went up with that one and that's about nuts.
Now you manage to go through every one in here and get it.
And at the end return that's value.
So let make clear all of these and just quickly talk about once again.
So what's happening in here is that we have a set of characters we're going to find out how many they
are.
Once we found out how many they are we're going to go from the top and the using of white and up we're
going to stop trying to 1 at the very beginning simply because remember if testing thinking is 25.
Their starting point of them is 24.
So we first subtract one from them and then we find the range based on that number and of one more character
we find that particular character.
We added two hours just so that things are going to become a little bit easier.
I'm going to go ahead and print this value.
You're going to have to do this.
I'm going to say print.
What do you have at every given time for us.
So as we go through this process print it for me.
Now I have managed to add in your category to a string called to reverse it.
If I go to Maine and say import import then is a in reverse it I can go in here and say well and it's
a string.
Reverse the string is.
Hello Esti are reversed it's now reverse it is actually working just like any function from the strings
because it has been added to the string at the implementation level of the string.
There is now a new function.
And now I can go in here and say well show me what is reverse it's so I'm going to say and Aslaug slog
reverse these.
And what is it going to be is going to be this.
And since I know this guy is printing some random stuff I'm actually going to push this down a few lines
so I'm going to sing a few lines down.
So let's run it.
Now it should do what you expected it to do.
Hopefully should say OK.
So first of all it says reverse it is the reverse of not the exclamation mark the L or O.W. so on and
so forth.
And this is the process that went through.
It said The first one then the next one then the next one then the next one all the way to the end.
It found all of them so that worked the way we expected it.
This is how you add categories to objective c which is essentially a way of marginalizing or code.
If that class belongs to loss or a way of adding new functions to our existing classes if we can not
change them.
So and as a string and as array I don't know a text field.
They're all kind of machine classes and if you want to add functionality to them we add them using categories.
So let's keep it up to here and we'll move on to our next lesson.