1
00:00:00,090 --> 00:00:05,430
So will come back again in this lesson and we going to talk about class extensions and what a class

2
00:00:05,440 --> 00:00:11,800
extension is is essentially exactly the same thing as a category you know Objective C but for swift.

3
00:00:11,800 --> 00:00:18,180
So if you use classic stations to add new features to existing classes or the classes that we don't

4
00:00:18,180 --> 00:00:24,750
have access to all the classic stations to add new features to our own classes simply for the purpose

5
00:00:24,750 --> 00:00:27,000
of marginalizing our code.

6
00:00:27,150 --> 00:00:31,880
So in this season I'm going to show you how to use glass extensions which is going to be rather simple

7
00:00:32,120 --> 00:00:37,020
and then I'm going to show you a picture essentially of an app that I'm developing myself and you can

8
00:00:37,020 --> 00:00:43,590
see how widely I'm using extensions just so that you know I make sure that my code is simple and readable

9
00:00:43,960 --> 00:00:49,240
and my colleagues who are working with me on the app or so easily understand my process.

10
00:00:49,250 --> 00:00:50,840
So go ahead and do it.

11
00:00:51,060 --> 00:00:59,300
And I'm going to start a new project this time a command line for swift and I'm going to call it class

12
00:00:59,390 --> 00:01:00,950
extensions.

13
00:01:02,570 --> 00:01:06,530
And then in here cause extensions are very very simple.

14
00:01:06,530 --> 00:01:08,050
Here is what we do.

15
00:01:08,090 --> 00:01:13,910
I'm going to go ahead and say I have a class on that class I'm going to call it my class doesn't have

16
00:01:13,920 --> 00:01:16,180
a superclass or does have superclass.

17
00:01:16,190 --> 00:01:19,770
It doesn't not function do something.

18
00:01:20,030 --> 00:01:23,800
And that's again very simple something you have seen many many times.

19
00:01:23,860 --> 00:01:30,950
It has a function that does something and then I'm going to go ahead and say extension and this extension

20
00:01:30,950 --> 00:01:36,460
is for my class on my extension has a function let's call it.

21
00:01:36,470 --> 00:01:39,030
Do I not think so.

22
00:01:39,190 --> 00:01:40,160
That's about that.

23
00:01:40,160 --> 00:01:48,220
I have a clause on that clause has an extension and if I go ahead and say let's use this by saying that

24
00:01:48,730 --> 00:01:52,190
my object is my class.

25
00:01:52,190 --> 00:01:58,450
So I made an object of that class and I can simply go ahead and say my object d'art.

26
00:01:58,470 --> 00:02:03,830
Now we have access to both of these functions the function from the actual cause of the function from

27
00:02:03,830 --> 00:02:08,140
the extension class and that's about that that's how you do extensions.

28
00:02:08,140 --> 00:02:11,000
You're right extension the name of your class and then you are.

29
00:02:11,030 --> 00:02:17,660
It is as if you're inside the body of that class and you are writing your stuff whichever way you like

30
00:02:17,660 --> 00:02:18,480
it to be.

31
00:02:18,500 --> 00:02:25,030
One thing that you have to keep in mind is that you can do something like this for instance the channel

32
00:02:25,120 --> 00:02:28,790
say I have a string between the body of the extension.

33
00:02:28,820 --> 00:02:35,740
These kind of things the stored properties you must have them in the original class dictionary.

34
00:02:35,750 --> 00:02:37,570
So you have to actually do it here.

35
00:02:37,760 --> 00:02:46,190
Primarily we use or at least I use class extensions for situations when I want to do functions if I

36
00:02:46,190 --> 00:02:52,900
want to add extra functions there and particularly for when I want to do something called a class function.

37
00:02:52,940 --> 00:02:55,760
That's something I want to show you in the next lesson up.

38
00:02:55,790 --> 00:03:02,350
This error obviously you know what's happening is that this object isn't explicitly arms wrapped optional.

39
00:03:02,360 --> 00:03:05,450
I'm saying well you need an initialization.

40
00:03:05,450 --> 00:03:10,640
I'm going to say if this was the case just to silence that error I'm going to say you with a default

41
00:03:10,640 --> 00:03:11,540
value.

42
00:03:11,540 --> 00:03:13,250
So that is extension.

43
00:03:13,250 --> 00:03:18,870
Now let's go ahead then use extensions on an existing class something that is needed.

44
00:03:18,890 --> 00:03:19,380
Right.

45
00:03:19,400 --> 00:03:24,530
So I'm going to go and I don't say remove all of these say extension strings.

46
00:03:24,710 --> 00:03:29,900
So I want to add a new feature or a new function with string class.

47
00:03:29,900 --> 00:03:32,160
And the thing I want to add is this.

48
00:03:32,240 --> 00:03:36,420
What we did before which was numbers only.

49
00:03:36,440 --> 00:03:43,850
So it's going to be much easier put in the lesson that we delta-V textfield delegates here and in here

50
00:03:43,940 --> 00:03:47,060
I had this function and then copy all of these.

51
00:03:47,060 --> 00:03:52,970
Of course we have to change some of it but there's a matter I'm going to bring it to my new extension

52
00:03:53,060 --> 00:04:00,370
on say that is a function and this function is called numbers only.

53
00:04:00,680 --> 00:04:09,580
And what is it going to do what is going to return as Thring which is only the numbers of that that

54
00:04:09,880 --> 00:04:11,040
the input value.

55
00:04:11,150 --> 00:04:16,760
So I'm going to go in here say that numbers is that remove characters that is purrfect separated them

56
00:04:16,760 --> 00:04:17,580
terrified.

57
00:04:17,750 --> 00:04:18,380
Let's join.

58
00:04:18,380 --> 00:04:19,900
Blah blah that's perfect.

59
00:04:19,910 --> 00:04:21,570
So what I want to do is this.

60
00:04:21,610 --> 00:04:22,940
And the ending here.

61
00:04:23,210 --> 00:04:26,310
I want to say to return what do I want in return.

62
00:04:26,330 --> 00:04:28,530
I want return the joined version.

63
00:04:28,580 --> 00:04:34,340
So I want to get the characters in something and then that thing I want to make sure that they return

64
00:04:34,340 --> 00:04:36,690
the one that only has numbers.

65
00:04:36,770 --> 00:04:41,360
Now what is that thing that they want to get from that is itself.

66
00:04:41,570 --> 00:04:43,930
Because self is this a string.

67
00:04:43,940 --> 00:04:47,740
So I'm going to get this stuff from self bracket's the sky.

68
00:04:47,780 --> 00:04:54,230
There's set everything that is not and numbers and then add them to each other and then return that

69
00:04:54,230 --> 00:04:54,650
one.

70
00:04:54,800 --> 00:05:01,000
So now I have this function let me get rid the all the comments it's easy and I can you know zoom into

71
00:05:01,010 --> 00:05:08,600
the whole thing easier on that I'm gonna go ahead and say I have variable mightest are which is the

72
00:05:08,600 --> 00:05:12,410
type of string and it is actually this that I've got.

73
00:05:12,450 --> 00:05:16,150
There's a lot of numbers of random characters.

74
00:05:16,370 --> 00:05:21,390
I'm going to save prints numbers only.

75
00:05:21,520 --> 00:05:25,530
Is what is that that is going to be.

76
00:05:25,700 --> 00:05:26,830
Boy I need a semicolon.

77
00:05:26,840 --> 00:05:33,730
I'm typing back and forth between Objective-C and sweet and this is going to be my s are I've got.

78
00:05:33,800 --> 00:05:36,330
What is it on numbers only.

79
00:05:36,560 --> 00:05:44,450
So I can actually go ahead and say hey get my string reaches a string and apply a function called numbers

80
00:05:44,510 --> 00:05:44,920
only.

81
00:05:44,930 --> 00:05:50,340
And this function only works here because I have the extension for a string in here.

82
00:05:50,420 --> 00:05:51,140
You fit on this.

83
00:05:51,140 --> 00:05:54,370
Now we should be able to see exactly what we expected to see

84
00:05:58,290 --> 00:06:05,540
say numbers only is these parts are we shouldn't have any way of making short of it both is five for

85
00:06:05,570 --> 00:06:09,050
eight Yes nine eight and for six.

86
00:06:09,060 --> 00:06:11,840
And then at the end it got to 500 on 60.

87
00:06:12,030 --> 00:06:12,620
Perfect.

88
00:06:12,630 --> 00:06:15,360
So that is how you got extensions.

89
00:06:15,360 --> 00:06:17,940
Extensions are very very common.

90
00:06:18,290 --> 00:06:21,340
Like I promised I'm going to show you one app of mine.

91
00:06:21,600 --> 00:06:23,930
Here's the OP you're kind of halfway through.

92
00:06:23,930 --> 00:06:26,700
As you can imagine that is so much is still happening.

93
00:06:26,700 --> 00:06:30,030
And we haven't finished a lot of the different things in here.

94
00:06:30,120 --> 00:06:37,560
But as you can see every class is broken into so many many extensions simply because I want my code

95
00:06:37,560 --> 00:06:43,680
to be easier and easy easily readable for my colleagues on you know anyone else who might be accessing

96
00:06:43,680 --> 00:06:43,850
these.

97
00:06:43,840 --> 00:06:50,250
So for instance if I go through a cloud that reads single Whatever is all the code that there is in

98
00:06:50,250 --> 00:06:51,110
this one fine.

99
00:06:51,120 --> 00:06:53,380
This is all the code that is in here.

100
00:06:53,730 --> 00:06:55,450
This is all the code that is in here.

101
00:06:55,480 --> 00:06:57,920
It makes things a lot easier for me.

102
00:06:57,960 --> 00:07:04,490
Let me show you another one that kind of you know perhaps makes easier sense and a simpler one here

103
00:07:04,530 --> 00:07:10,110
for instance I have a property view controller that is you know a view controller like the ones we have

104
00:07:10,110 --> 00:07:11,190
seen in the past.

105
00:07:11,280 --> 00:07:16,710
And then my property viewpoint you to has something that at some borders it has something that adds

106
00:07:16,710 --> 00:07:17,740
that content to.

107
00:07:17,760 --> 00:07:19,930
It has something to do with the map.

108
00:07:19,980 --> 00:07:22,670
It has something to do with the labels in it.

109
00:07:22,680 --> 00:07:29,400
But each of them has its own extension simply because by having the extensions I make sure that my code

110
00:07:29,400 --> 00:07:33,960
is far more modularized and it's also a lot easier for everybody else to read it.

111
00:07:33,960 --> 00:07:39,330
If I tell a colleague of mine that there seems to be an issue with the map in the proper The View Controller

112
00:07:39,630 --> 00:07:44,790
they know they have to immediately coming here and they work on this one file which really makes our

113
00:07:44,790 --> 00:07:46,190
lives a lot easier.

114
00:07:46,200 --> 00:07:52,680
Specifically when we were working together on larger projects we did see these in the future as well

115
00:07:52,740 --> 00:07:58,100
when we work on version control on how you have to you know essentially go with the best the strategies

116
00:07:58,140 --> 00:08:02,490
of making sure that you and other people can work easily with each other.

117
00:08:02,490 --> 00:08:06,120
So with that in mind let's move on to our next lesson.

118
00:08:06,120 --> 00:08:09,240
This was extensions and I'll see you in the next line.
