Indices I'm going to show you how to make classes in Objective C in an ideal situation I would leave
this up to you as an assignment or an exercise.
But I think it helps if I give you some instructions at the beginning about classes in Objective-C.
In theory they do exactly the same thing as the ones in swift.
In practice they are a little bit different in how we called them.
So let's go ahead to X go at the next one I'm going to start a new project command line.
At this time is going to be an objective see one and I'm going to say this is called classes in object.
See so the one big difference between when we do things in Objective science free is if you remember
in objective C-v always had two files we had the dot h n m.
And that's kind of for a reason that's there because they are to declare classes in two separate places.
So to declare a class in Objective-C I'm going to say at interface and then my class name I'm going
to call this one class to be a car for instance.
And then unlike Strief classes in Objective-C absolutely need to have a super class or what we call
a base class.
Now a base class is essentially something that we inherit from.
For instance you might be writing something that is a new feature to an array and then this would be
common in this array.
I'm trying to make a subclass of an array.
If you don't know what subclass you have to use it's not exactly scientific.
But as a rule of thumb you can say you don't know what's the superclass.
I can stick to a subject but most likely you wouldn't know what to use.
For instance when we make an appt.
You don't need to do this part.
I'm just going to show it to you.
Then we make a template up.
If you remember our interfaces in our view controller are subclasses of you eye view controller simply
because my view contouring needs to inherit things from your view controller.
We'll talk about this more when we talk about inheritance there's going to be a lesson about that.
But for now if you're making an interface and you don't know the superclass of it you can live with
a subject.
Once you have a class interface or you have your class figuration.
I'm going to have to go ahead and add an implementation.
And that's essentially saying what are the different a specification of my card.
This is exactly like the one that I'm going to reopen this.
Again is exactly like the one that we have in view controller data and that's the implementation of
our view controller.
If you remember I told you we can get rid of this private interface and then we have the implementation
view controller and we have the interface View Controller.
So in here we have the same thing.
Once we have an interface of car we have the implementation of the car.
He's asking me to write the methods for it.
So let's go ahead and add a few properties or attributes to our core and then some methods.
The first property that I want to add and we are like this property and this thing is going to be called
a name that is the name of the car object we're about to make.
If this was a real car in let's say a game that you're writing it would have model and color is speed
a number of years so on and so forth for now and then have only one more and that is property that's
going to be a boolean property is called ease on which essentially defines whether the car is currently
on or not.
I'm also going to add two functions or two methods to our cars.
I'm going to say one of them is art.
The engine on.
So that's one method that's going to turn the engine on and they want to have an in it I want it initialises
I want any show they want to override my initializers.
So this one is a little bit different than what we had in in swift so in here.
As always when we are writing something we have to actually tell it what kind of return type it has
and the return type of it will be something that we call instance type means of whatever is the type
of this car return that same it means when you are writing this method if it is going to be or initialiser
return it of the same type.
The next thing which is very important it has to begin with in it its name has to have in it.
And then you can add whatever you like and will say in it with the state on what's the estate.
The estate is a Boolean OR they call it our current or is beginning a start a state.
So that's essentially initializing a car with a beginning or a starting state.
Imagine you have your car on at the very beginning or you want the engine to be off at the very very
beginning.
Now so far and I think that this may drop that.
So far we have managed to write an interface for a car a class declaration for car two attributes and
two functions one of the two functions is in its function.
The other one is just a function that does something.
So now Isaac squaddies right reminding us is saying that the definition for these two methods are not
there.
His things are what you want to do with these two methods.
So I'm going to say well one of them let me copy both of them actually but easier to copy than to retype
everything.
I'm going to say in both of them I want to do some things.
So one of them turn engine on is actually very easy.
I want to say whenever this function gets called ease on it becomes true.
So if the engine is off it becomes on even if it is on stay still comes on.
Now the other method the initialization needs a little bit of explanation.
First of all it has to return something to the team.
It has to return is essentially going to be of the same type or it is going to be self so I'm going
to say first of all and that's the end return.
But then we have something very simple which is is on has to become the very start that we have.
If you want the engine to start on that would become on if you wanted to be off that would become off
I would become false essentially.
The last thing which is actually here the first thing you have to do we have to say self is super super
in it and that's simply because our class actually has a superclass.
So we have to say in a nutshell when you are initializing first initialize this object now that in a
subject and here is I'm kind of bleeding into the inheritance here we are saying car is an object that
has a name and he's on.
So I'm saying first of all make out on a subject to initialize that once you did that for this instance.
Change is on to something new and then return it to us.
And that's about that.
That's everything we needed to do.
So really quickly keep an eye on this so you know what's going on.
Once you are happy with the small one to initialising this for the first time.
So now I'm going to go to my actual program I'm going to say color on this car is called a jeep and
this is going to be car lock and in it it could either do it like that or you could say car.
Let's call this one a whole lot.
And that's going to be car lock and you need to get a state.
And I'm going to say Drew.
So both of them are valid.
Most of them are the ways that we could use them.
So that's how you declare a class in Objective-C.
That's how you introduce attributes to it.
That's how you do functions.
And this is in particular how you do and emits methods for it.
And then of course in the end it meant that you have to return the self and the self has to be whatever
it is to particular US which in Objective C is mandatory as opposed to Swift.
And then when you're using it this is like everything else that we have seen and as an array my array
is an array of locks.
And it.
Right.
That's exactly the same thing as we have in here.
Except here is called Car overlays and Asare or any other state.
And remember we had in it with objects we could actually add a stuffed with on the fly at the initialization
level.
So that's about that.
That's how we managed to add classes to our Objective-C program.
And that began to move one.