So well coming again in this listen we're going to talk about selectors and class types.
So select essentially a way for us to find out if a certain object can respond to a certain function
and class type is essentially used to find out the type of the class that has made this current object
essentially a way to find out which class has instantiated this particular object.
Now let's go to school and get this done.
And just so that you know every once in a while we go back to the whole objective see mode.
I'm going to do both of these in Objective-C this time.
So in school I'm going to go ahead and say Give me a new command line tool in Objective-C and it's got
selectors Objective-C
now in my heart.
Emma I'm going to go ahead and say I have something called an s mutable array called my array which
is and is mutable.
Array new is just a brand new allocator initialized array.
Then I'm going to go ahead and do this.
I'm going to say my array perform selector Vitt object and the selector they want to apply to it is
called that selector that is called add object for instance and the object that when out is perhaps
mining.
That's all I want to do in here.
Now you might be asking yourself Is this a safe way of doing it.
Are you just assuming that mutable array no matter what can take an object to it.
And the answer to that is no it actually another very safe way of doing it and much safer way would
be nice if I said if my array responds to selector and this I want to use is called selector add object
if that is true then I want to do this whole thing.
Otherwise I don't mind waiting.
So that's about that if my error rate response to this selector then do that thing add to it for instance
a value and that's pretty much all there is to a selector in the past you have seen selectors that I
as an Baden's For instance I said My.
Then as the target target was self with selector and the selector was at select store on name of functions
so on and so forth.
And that's how we use selectors to make sure that something does respond to a certain function not a
much bigger question for you might be so.
I know this is an array and I know it can have an object.
So what's the point of using this.
So the answer to that would be in a situation like this when we have an array of objects that we don't
know what they are.
So let's go ahead and make that and I'm actually going to make this a slightly more broader application
so I can use it for class type husband.
So I'm glad you don't say command and I'm going to make a cocoa class of the type in its object language
is objective c I'm going to call it car.
So I have a daughter age and a dot M for a car.
And I also have at that age that am for a person and I have a last command an adult age an adult am
for let's say a building.
So cars buildings and people.
And we have three classes in here.
Now I'm going to go do some clean up in all of those.
So you know things are a lot easier for us to see and to understand that that ends I don't have anything
into that.
Again I'm going to remove the nullable again.
And then the same thing in year is going to be rather simple so don't worry about having too many files
and everything is going to be much easier.
So the first thing I want to do is I want to go to the car at that age and say every one of these classes
says they have a method called the law Kate.
So that's essentially going to place my objects my cards my people my whatever in some place.
Also of course they have something called Change gear of not like that.
Not like that change.
So they have two methods and get a copy that I'm going to go to my dad and doesn't have to have an actual
implementation.
As long as we have the curly brackets so that's that.
I'm going to go to my person and say well my person does have it ok but cannot change gear but can't
eat somebody copy these to go to my person that am on say do the same thing have a body for the functions
so you don't get an error when you go to the building.
The building they have it.
And they can not have something that does eat but they can have something called Lights on the lights
they turn on them and they copy those and go to the top.
And of course the same thing as always.
So now I have three classes that the three of them they have a method in them called locate bought each
of them has one extra metadata and yet on the other one lights on.
Now I'm going to go to my dad and I'm going to import all three of them and when I say import or whatever
name port is call and I want to go ahead and say building and I want to go ahead and say person.
So all three of them are either in here and in my array before I do any of these things I'm going to
go ahead and make three copies.
I'm going to say car let's say Jeep is going to be our car.
You know I have one jeep.
My copy is twice even though that was a very good idea person myself is person you're building.
Let's call it of Empire State.
I don't know a name of any buildings to be honest.
All the buildings are numbered like the number 226 or whatever.
So that's about that.
So I have a car.
I have a person and I have a building and they're all added to my array just so that they can make things
easier.
I'm going to cut them up here paste them in here and say I look on to the objects and the objects are.
I mean the Empire State Building.
Perfect.
So now I have an array with mixed objects so I don't know what they are.
I don't know.
You know what do they actually.
What can they do for now.
I could go ahead and say for Gints my ears 0 is a smaller than my array that counts.
And then I press plus do this.
What am I going to do.
I'm going to say if my array object that index responds to selector and let's say the selector that
I want to use it.
If it does respond to it which is this then it's down and eat.
So while this example may not really do anything for us it explains to you why we would end up using
selectors.
We use it for when we don't know what the object is what is it capable of it might be useful.
And you know whether they do respond to a certain select or they don't.
So while we are here let me put on another for loop that is going to tell us what is the class of each
of the guys in my array.
So I'm going to go ahead and say for now here's a very interesting thing.
I don't want to use a numeric for loop I want to use a for loop.
You know usually if you say four and a string for instance Each my array or so on and so forth.
But I know the objects in my array are not unnecessary.
Not only that they could be anything.
So in a situation like that I'm going to say valid for ID.
Each in my array it means go through the array.
Irrelevant of what is inside.
Then I'm going to say for ID each in my array.
Let's do this.
If so what is that for each.
Is kind of class.
And what's the class that you're looking for.
The class is going to be that I'm looking for the ones that are of the type of building I'm going to
say building class if they are of the type building class here you can do whatever you want to do with
the building.
For instance building or turn lights on or anything to that effect.
So that's about that.
So we managed to have it respond to select to find out if these objects respond to a certain thing and
we managed to get the kind of the class that any object is.
And then depending on that you will end up doing things you could know whether it is or whether it is
not.
If you want to filter certain objects out and it's sometimes useful to know what's the kind of the class
that you are dealing the object that you're dealing with.
So that's about that for selectors is kind of class.
Let's move on.