1
00:00:00,500 --> 00:00:06,410
This or some are going to talk about class methods class methods are a very useful way for module raising

2
00:00:06,430 --> 00:00:11,700
record as well as making sure that you don't have a large footprint on your memory and they're actually

3
00:00:11,700 --> 00:00:12,930
very simple to use.

4
00:00:12,930 --> 00:00:19,140
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

5
00:00:19,140 --> 00:00:22,040
to call it class methods.

6
00:00:22,050 --> 00:00:30,480
SWIFT Yeah I'm going to go ahead and say I have one class called class let's call it my class.

7
00:00:30,660 --> 00:00:36,990
And then this class of mine it has only one function that function it doesn't even matter it says do

8
00:00:36,990 --> 00:00:42,030
something and doesn't have any promises doesn't have any return type doesn't have any body we just know

9
00:00:42,030 --> 00:00:47,940
that this thing actually exist not you know for a fact that so far if you want to use this function

10
00:00:47,970 --> 00:00:55,750
the only solution was this I had to go ahead and say let's my objects become my class.

11
00:00:56,010 --> 00:01:00,850
And then of course my object could do that thing do something now.

12
00:01:00,860 --> 00:01:02,890
Well there's nothing wrong with this approach.

13
00:01:02,890 --> 00:01:05,520
There is however a challenge for that and that challenges.

14
00:01:05,660 --> 00:01:11,790
I have already allocated a space to my object and that object is not going anywhere until this cycle

15
00:01:11,790 --> 00:01:18,360
finishes whether we go out of this control or this object gets destroyed or any other way that we will

16
00:01:18,360 --> 00:01:22,980
talk about it in a few lessons about memory management and reference counting.

17
00:01:22,980 --> 00:01:27,660
But the problem is this if you have an object that we just wanted to use this method to fit what we

18
00:01:27,660 --> 00:01:30,620
had to actually instantiate it from that class.

19
00:01:30,720 --> 00:01:32,710
And that's not really a good approach.

20
00:01:32,710 --> 00:01:39,810
What if there was a way for us we could only be called the function without ever needing to instantiate

21
00:01:39,810 --> 00:01:40,700
an object for it.

22
00:01:40,740 --> 00:01:43,580
And that is then and that's a very simple solution.

23
00:01:43,630 --> 00:01:46,340
You just convert this into a class function.

24
00:01:46,440 --> 00:01:49,780
And the moment it becomes a class function that becomes a lot easier.

25
00:01:49,800 --> 00:01:56,880
All you have to do is to say my class that do something and then it goes and does this thing and that's

26
00:01:56,880 --> 00:01:57,280
it.

27
00:01:57,310 --> 00:02:04,740
And there's no footprint we haven't instantiated object it just runs that one function.

28
00:02:04,740 --> 00:02:09,450
So that's about that class functions are in the practices that comes up.

29
00:02:09,450 --> 00:02:14,940
I will be using a lot of class functions keeping in mind that whenever you feel like you're actually

30
00:02:15,660 --> 00:02:22,020
allocating an object just so that you can call a method that seems like a very good place for you to

31
00:02:22,390 --> 00:02:22,860
try.

32
00:02:22,860 --> 00:02:26,610
Class functions instead of just functions one of you are here.

33
00:02:26,610 --> 00:02:31,950
I'm going to quickly read this in Objective-C as well because it's a little different over there.

34
00:02:31,950 --> 00:02:43,500
So I'm going to say class methods Objective-C class methods Objective-C as you can imagine up here.

35
00:02:43,530 --> 00:02:52,690
I have a class interface and that itself is I'm going to call it my class which is an object for instance.

36
00:02:52,820 --> 00:03:00,670
And as usual it has one function called do some something and that's about it.

37
00:03:00,690 --> 00:03:02,590
Of course you have to implement it.

38
00:03:02,730 --> 00:03:10,050
Implementation my class and do something will be there as of now so far.

39
00:03:10,050 --> 00:03:13,050
Again just like the one that we did for Swift.

40
00:03:13,050 --> 00:03:14,680
We have to do this.

41
00:03:14,790 --> 00:03:24,750
I had to go to my main saying my class my object who's going to be my class mule.

42
00:03:25,140 --> 00:03:33,900
And then my object could call or do something which is again exactly the same thing that it did in a

43
00:03:33,900 --> 00:03:40,350
swift and much easier way would be converting this to become a class method by changing its signature

44
00:03:40,350 --> 00:03:42,450
from a dash to a plus sign.

45
00:03:42,630 --> 00:03:44,600
So now it has got the plus sign.

46
00:03:44,610 --> 00:03:51,970
It becomes essentially a class method which means I could go ahead and say my class do something.

47
00:03:52,110 --> 00:03:55,750
And this is just going to call that function from here.

48
00:03:55,750 --> 00:03:58,950
That plus sign sometimes because the constructor meathook.

49
00:03:59,070 --> 00:04:03,810
Because that's essentially how you call methods on a class to build them.

50
00:04:03,810 --> 00:04:11,840
For instance if I had an x ray my array is an S array block.

51
00:04:12,000 --> 00:04:18,520
Then I had a kind of structure of matter called init with objects that returns of my instance time don't

52
00:04:18,570 --> 00:04:24,480
return that or just return void but they can call that like kick ass method and they can actually do

53
00:04:24,480 --> 00:04:28,590
something that they want to do on my class using this method.

54
00:04:28,590 --> 00:04:34,350
Of course if you want to access properties from my class Dorce properties they have to be a static properties

55
00:04:34,650 --> 00:04:40,090
and we'll talk about that in a few lessons when we talk about the reference counting.

56
00:04:40,140 --> 00:04:45,690
But for now this is how you would declare an undefined class functions that Objective-C.

57
00:04:45,780 --> 00:04:50,360
This is how you do it in swift and I shall see you in the next lesson.
