This or some are going to talk about class methods class methods are a very useful way for module raising
record as well as making sure that you don't have a large footprint on your memory and they're actually
very simple to use.
So let's go ahead and do it so I guess what I'm going to have a new project command line tool I want
to call it class methods.
SWIFT Yeah I'm going to go ahead and say I have one class called class let's call it my class.
And then this class of mine it has only one function that function it doesn't even matter it says do
something and doesn't have any promises doesn't have any return type doesn't have any body we just know
that this thing actually exist not you know for a fact that so far if you want to use this function
the only solution was this I had to go ahead and say let's my objects become my class.
And then of course my object could do that thing do something now.
Well there's nothing wrong with this approach.
There is however a challenge for that and that challenges.
I have already allocated a space to my object and that object is not going anywhere until this cycle
finishes whether we go out of this control or this object gets destroyed or any other way that we will
talk about it in a few lessons about memory management and reference counting.
But the problem is this if you have an object that we just wanted to use this method to fit what we
had to actually instantiate it from that class.
And that's not really a good approach.
What if there was a way for us we could only be called the function without ever needing to instantiate
an object for it.
And that is then and that's a very simple solution.
You just convert this into a class function.
And the moment it becomes a class function that becomes a lot easier.
All you have to do is to say my class that do something and then it goes and does this thing and that's
it.
And there's no footprint we haven't instantiated object it just runs that one function.
So that's about that class functions are in the practices that comes up.
I will be using a lot of class functions keeping in mind that whenever you feel like you're actually
allocating an object just so that you can call a method that seems like a very good place for you to
try.
Class functions instead of just functions one of you are here.
I'm going to quickly read this in Objective-C as well because it's a little different over there.
So I'm going to say class methods Objective-C class methods Objective-C as you can imagine up here.
I have a class interface and that itself is I'm going to call it my class which is an object for instance.
And as usual it has one function called do some something and that's about it.
Of course you have to implement it.
Implementation my class and do something will be there as of now so far.
Again just like the one that we did for Swift.
We have to do this.
I had to go to my main saying my class my object who's going to be my class mule.
And then my object could call or do something which is again exactly the same thing that it did in a
swift and much easier way would be converting this to become a class method by changing its signature
from a dash to a plus sign.
So now it has got the plus sign.
It becomes essentially a class method which means I could go ahead and say my class do something.
And this is just going to call that function from here.
That plus sign sometimes because the constructor meathook.
Because that's essentially how you call methods on a class to build them.
For instance if I had an x ray my array is an S array block.
Then I had a kind of structure of matter called init with objects that returns of my instance time don't
return that or just return void but they can call that like kick ass method and they can actually do
something that they want to do on my class using this method.
Of course if you want to access properties from my class Dorce properties they have to be a static properties
and we'll talk about that in a few lessons when we talk about the reference counting.
But for now this is how you would declare an undefined class functions that Objective-C.
This is how you do it in swift and I shall see you in the next lesson.