1
00:00:08,190 --> 00:00:11,190
Hey everybody has gone this is Caleb with slopestyle.

2
00:00:11,430 --> 00:00:17,250
And in this video we're going to be talking about extensions in swift now extensions are amazing because

3
00:00:17,250 --> 00:00:23,940
they allow you to add functionality to an existing class structure an enumeration or even a protocol.

4
00:00:24,000 --> 00:00:24,240
OK.

5
00:00:24,240 --> 00:00:30,480
There's lots of different things we can do to extend the capabilities of certain classes either or classes

6
00:00:30,480 --> 00:00:35,000
that have already been created by us or even classes we create ourselves.

7
00:00:35,010 --> 00:00:36,950
Now this video is going to be an introduction.

8
00:00:36,960 --> 00:00:42,190
We're going to be using a playground and I want to show you a few cool things you can do with extensions.

9
00:00:42,300 --> 00:00:47,880
And then in the next video we're actually going to build a very very simple app to show you how extensions

10
00:00:47,880 --> 00:00:49,850
could be useful to you as a developer.

11
00:00:49,860 --> 00:00:56,070
So let's go ahead and let's pull open X code and let's go ahead and get started with the new playground.

12
00:00:56,160 --> 00:00:57,540
So click that button.

13
00:00:57,960 --> 00:01:00,840
And of course it's going to pop open on the wrong window.

14
00:01:00,840 --> 00:01:01,950
There we go.

15
00:01:01,950 --> 00:01:11,860
All right click blank and let's call this extension is fun time.

16
00:01:13,970 --> 00:01:14,380
Yeah.

17
00:01:14,590 --> 00:01:15,710
No just yet.

18
00:01:15,800 --> 00:01:17,430
Let's do extension's funtime.

19
00:01:17,780 --> 00:01:24,090
OK click Create and we get ourselves a nice little playground to play in.

20
00:01:24,130 --> 00:01:26,610
So let's go ahead and let's do that.

21
00:01:26,620 --> 00:01:32,350
So first of all I'm going to show you the syntax for an extension and to do that I'm going to get rid

22
00:01:32,350 --> 00:01:35,050
of that and get rid of this silly comment at the top.

23
00:01:35,050 --> 00:01:36,200
We don't need.

24
00:01:36,460 --> 00:01:42,530
And so let's begin to create an extension what we need to do is we need to type extension.

25
00:01:42,790 --> 00:01:44,120
OK believe it or not.

26
00:01:44,260 --> 00:01:52,870
And an extension like I said allows us to extend an existing class a structure a protocol or an enumeration.

27
00:01:52,870 --> 00:01:56,440
So what we're going to do is to create an extension.

28
00:01:56,440 --> 00:02:01,210
We're going to extend it like you're going to extend to certain classes let's think of a class and swift

29
00:02:01,210 --> 00:02:04,920
How about integers k so extension int.

30
00:02:04,930 --> 00:02:08,350
We're going to extend the int subclass.

31
00:02:08,410 --> 00:02:08,990
OK.

32
00:02:09,580 --> 00:02:14,530
And you set it up just like you would a class or a function you put a set of curly brackets and that's

33
00:02:14,530 --> 00:02:16,980
where all of the important information goes inside.

34
00:02:16,990 --> 00:02:18,770
So that's how an extension is made.

35
00:02:18,790 --> 00:02:24,750
Now what if you had created your own class like class my class.

36
00:02:24,850 --> 00:02:25,540
OK.

37
00:02:25,900 --> 00:02:30,370
You can also extend your own custom classes and make your own extensions.

38
00:02:30,370 --> 00:02:33,430
And inside is where all the functionality is going to go.

39
00:02:33,600 --> 00:02:33,960
OK.

40
00:02:34,000 --> 00:02:34,510
Whoops.

41
00:02:34,560 --> 00:02:35,530
Functionality.

42
00:02:35,770 --> 00:02:41,530
OK so we're going to add some functions and lots of cool stuff to extend the capabilities of a certain

43
00:02:41,530 --> 00:02:42,270
class.

44
00:02:42,450 --> 00:02:43,010
OK.

45
00:02:43,210 --> 00:02:51,190
Now some of the things that extensions are able to do are they are able to add computed properties and

46
00:02:51,190 --> 00:02:52,720
computed type properties.

47
00:02:52,720 --> 00:02:55,600
They're able to define instance and type methods.

48
00:02:55,660 --> 00:03:00,490
They're actually able to provide new initializers So if you think about when you create something with

49
00:03:00,490 --> 00:03:06,030
an initializer like a C.G. wrecked car you have lots of different ways to initialize it.

50
00:03:06,070 --> 00:03:07,810
That's just something you can do with extensions.

51
00:03:07,810 --> 00:03:08,620
Pretty cool.

52
00:03:08,620 --> 00:03:13,580
You can also define and use subscripts as well as nested types which is pretty cool.

53
00:03:13,780 --> 00:03:18,000
And you're also able to make an existing type conform to a certain protocol.

54
00:03:18,010 --> 00:03:21,060
So lots of different things you can do with extensions.

55
00:03:21,100 --> 00:03:27,690
And so now let's go ahead and let's take a look at just one way we could extend an existing swift class.

56
00:03:27,730 --> 00:03:34,440
So let's go ahead let's make an extension of string type extension string.

57
00:03:35,020 --> 00:03:40,120
And what we're going to do is we're going to build our own function that's going to be able to reverse

58
00:03:40,240 --> 00:03:40,940
a string.

59
00:03:41,060 --> 00:03:41,290
OK.

60
00:03:41,290 --> 00:03:42,840
Now there already is one.

61
00:03:42,880 --> 00:03:43,650
I know that.

62
00:03:43,810 --> 00:03:47,970
But the interesting thing is I'll show you the difference later there is a difference.

63
00:03:48,220 --> 00:03:54,100
And we're going to basically write a function that will return the exact opposite of a string that we

64
00:03:54,100 --> 00:03:54,730
give it.

65
00:03:54,730 --> 00:04:00,660
So inside of the string extension go ahead and type phunk reverse.

66
00:04:01,240 --> 00:04:03,820
And you notice that pops up there reverse.

67
00:04:03,820 --> 00:04:10,060
There are the two functions that already exist within the string class reversed and it returns an array

68
00:04:10,060 --> 00:04:14,740
of character and reversed and it returns a reversed collection of type string.

69
00:04:14,740 --> 00:04:16,330
So those are the ones that already exist.

70
00:04:16,330 --> 00:04:20,370
But we're going to write our own simply called reverse.

71
00:04:20,380 --> 00:04:25,960
So what we're going to do is we're basically going to think of extension string here.

72
00:04:25,960 --> 00:04:32,450
Think of this as something that can be applied to any type of string.

73
00:04:32,470 --> 00:04:38,410
So inside of the extension let's say that we applied this function to a string that we've created inside

74
00:04:38,410 --> 00:04:38,950
the function.

75
00:04:38,950 --> 00:04:46,120
We can just call it self ok because this extension is referring to whatever instance of string this

76
00:04:46,120 --> 00:04:47,290
function is called upon.

77
00:04:47,290 --> 00:04:54,370
So we're going to be referring to self to refer to the string that this extension is is connecting to.

78
00:04:54,370 --> 00:04:56,970
So let's go ahead and let's write this function out.

79
00:04:57,100 --> 00:05:00,970
Oh you know what we need to actually return a string at the end right because we need the reverse string

80
00:05:00,970 --> 00:05:01,630
at the end.

81
00:05:01,900 --> 00:05:05,410
So let's go ahead and create an array of characters.

82
00:05:05,470 --> 00:05:08,690
So go ahead and type var character array.

83
00:05:09,250 --> 00:05:13,270
And that's going to be equal to an array of character.

84
00:05:13,270 --> 00:05:17,030
Like I said and let's just instantiate it for now.

85
00:05:17,110 --> 00:05:23,080
Now we're going to do is we're basically going to parse through and for every character in the string

86
00:05:23,080 --> 00:05:28,900
we've passed then we're going to go ahead and insert it into our character array at index 0 so that

87
00:05:28,930 --> 00:05:33,160
every single time a new character is added it's added to the front.

88
00:05:33,190 --> 00:05:37,460
And so therefore we will reverse the entire string as we pass through all the characters.

89
00:05:37,660 --> 00:05:42,370
So go ahead and type four character in.

90
00:05:42,490 --> 00:05:50,320
Remember we're referring to self because that's the instance of the string self dot characters we're

91
00:05:50,320 --> 00:05:56,290
going to parse through the array of characters that's already built into to a string for character self

92
00:05:56,350 --> 00:05:58,040
dumb characters.

93
00:05:58,630 --> 00:06:03,790
What we're going to do is we're going to go ahead and type character array and we're going to go ahead

94
00:06:03,820 --> 00:06:10,070
and call insert a new element at Int and that's like kind of like the index path of an array.

95
00:06:10,360 --> 00:06:11,840
So we're going to pass it in 0.

96
00:06:11,890 --> 00:06:16,810
Actually it is the index or because that's what we're dealing with we're going to insert whatever character

97
00:06:16,810 --> 00:06:22,600
we get through at index 0 so that every single new one is going to be passed in at 0 and it'll bump

98
00:06:22,600 --> 00:06:24,160
all the rest of the letters down.

99
00:06:24,190 --> 00:06:27,310
So the first character comes in and it's at index 0.

100
00:06:27,310 --> 00:06:29,290
The next one comes in and now it's at index 0.

101
00:06:29,290 --> 00:06:33,580
The next one comes and they all start to move down until the whole string is reversed.

102
00:06:33,580 --> 00:06:41,830
At the very end what we're going to do is we're going to return the character but remember it's an array

103
00:06:41,920 --> 00:06:44,520
of characters we need a string.

104
00:06:44,530 --> 00:06:45,700
So what we can do.

105
00:06:45,700 --> 00:06:46,580
Check this out.

106
00:06:46,600 --> 00:06:50,980
We can call string and when we instantiate a string.

107
00:06:51,040 --> 00:06:55,820
If you scroll down you can make a string from a sequence of characters.

108
00:06:55,870 --> 00:07:02,220
So select characters and we can pass in the character array because that's a sequence of characters.

109
00:07:02,230 --> 00:07:03,660
Pretty cool right.

110
00:07:03,670 --> 00:07:05,290
So this is one function.

111
00:07:05,290 --> 00:07:08,110
Now that is in our string extension.

112
00:07:08,230 --> 00:07:11,300
I want to show you how to use it now though it's really really cool.

113
00:07:11,320 --> 00:07:15,380
So what I'm going to do to show you this is I'm going to create a string.

114
00:07:15,460 --> 00:07:20,650
Let's just do var name and I'm going to say Marty McFly.

115
00:07:20,790 --> 00:07:23,470
OK and of course you can tell that that's a string.

116
00:07:23,470 --> 00:07:29,050
Now what I can do is because the variable name is of type string.

117
00:07:29,050 --> 00:07:32,650
I can call name dot reverse.

118
00:07:32,650 --> 00:07:34,910
Now be careful don't use the top two but look.

119
00:07:34,960 --> 00:07:36,160
Reverse without the D.

120
00:07:36,160 --> 00:07:37,680
That's our function.

121
00:07:37,690 --> 00:07:42,580
Anything that is a string we can call reverse on and check out what happens when we call it.

122
00:07:42,580 --> 00:07:44,020
Look at the print out here.

123
00:07:44,170 --> 00:07:46,660
We get a reversal of Marty McFly.

124
00:07:46,660 --> 00:07:49,330
The string is reversed it's really really cool.

125
00:07:49,330 --> 00:07:53,240
Now just to show you that this is only applicable to strings.

126
00:07:53,260 --> 00:08:00,550
I'm going to create an integer like 12 and if I try to call reverse on this integer it's not going to

127
00:08:00,550 --> 00:08:06,260
work because we only extended the string class not the integer class so therefore it doesn't work.

128
00:08:06,310 --> 00:08:12,100
That is one way you can extend string with a cool function that reverses any string that you attach

129
00:08:12,400 --> 00:08:15,350
that function to really really cool.

130
00:08:15,400 --> 00:08:18,590
Now we're going to go ahead and create an extension of it.

131
00:08:18,640 --> 00:08:25,620
So let's do that down here below let's give you some space here so you can see OK this looks good.

132
00:08:25,750 --> 00:08:26,020
All right.

133
00:08:26,020 --> 00:08:32,260
So go ahead and type extension and just like we did with the string and we're going to write a function

134
00:08:32,500 --> 00:08:37,450
that's going to basically allow us to square any value that we give it.

135
00:08:37,450 --> 00:08:44,170
So go ahead and type phunk square and inside of this function we're going to just go ahead and type

136
00:08:44,170 --> 00:08:51,010
self times self because that's what something squared is the value of it multiplied by that value.

137
00:08:51,010 --> 00:08:58,150
Now of course in order for this to actually do something we need to return a value of int loops of end

138
00:08:58,210 --> 00:09:02,840
so that we can actually use this on an integer.

139
00:09:02,980 --> 00:09:08,260
And of course since we need to return we'll just return self time self.

140
00:09:08,260 --> 00:09:09,910
So there's our extension.

141
00:09:09,940 --> 00:09:12,740
That's cool but let's go use it.

142
00:09:12,880 --> 00:09:18,020
What we can do is we can say var value equals nine.

143
00:09:18,490 --> 00:09:24,460
And now what I can do just like with the string is I can call value dot square and watch what happens

144
00:09:24,460 --> 00:09:26,560
when we print out in the terminal here.

145
00:09:28,320 --> 00:09:31,280
You can see it prints out 81 it's squared R value.

146
00:09:31,330 --> 00:09:32,490
That's really cool.

147
00:09:32,760 --> 00:09:37,560
But one of the most interesting things about extensions is that you're able to use what are called mutating

148
00:09:37,590 --> 00:09:44,610
functions and a mutating function is basically a function that is going to actually modify the value

149
00:09:44,610 --> 00:09:46,530
of the variable that it's called on.

150
00:09:46,530 --> 00:09:51,030
So right now var value equals nine k value is equal to nine.

151
00:09:51,240 --> 00:09:58,440
We call value Square and what it does is it takes the value of our variable here multiplies it by itself.

152
00:09:58,440 --> 00:10:02,600
And this right here value Square is currently equal to 81.

153
00:10:02,610 --> 00:10:08,340
But watch what happens if I print out value even below the function you'll notice it still comes back

154
00:10:08,370 --> 00:10:09,530
as 9.

155
00:10:09,540 --> 00:10:12,720
That's because we have not mutated the original value.

156
00:10:12,780 --> 00:10:16,680
We're going to do that now with a mutating function and that's something really cool about extensions

157
00:10:17,240 --> 00:10:24,030
is it's going to allow you to mutate a certain function K and to do that we're going to make a function

158
00:10:24,030 --> 00:10:26,350
that's going to return the area of a circle.

159
00:10:26,490 --> 00:10:27,060
OK.

160
00:10:27,420 --> 00:10:33,420
So let's go ahead and let's extend double K because we need some decimal places here because if you

161
00:10:33,420 --> 00:10:39,530
know the the function to determine the area of a circle is pi are squared right.

162
00:10:39,540 --> 00:10:44,600
We're gonna take the radius of the circle square it and then multiply it by pi.

163
00:10:44,610 --> 00:10:50,610
So go ahead and call extension double and we're using double again because we need numbers after the

164
00:10:50,610 --> 00:10:51,590
decimal point.

165
00:10:51,930 --> 00:10:54,630
And we're going to go ahead and write our area function here.

166
00:10:54,630 --> 00:11:04,970
So go ahead and type phunk area or you know how to calculate area that's a little more descriptive and

167
00:11:05,030 --> 00:11:07,510
let's create a constant to hold the value of pi.

168
00:11:07,520 --> 00:11:12,640
So A-type let pi equals three point 1 4 1 5 that's close enough.

169
00:11:12,650 --> 00:11:15,080
You know it's not exact but it's pretty good.

170
00:11:15,080 --> 00:11:26,860
And what we can do is we can say self is equal to pi times self time self k.

171
00:11:27,040 --> 00:11:34,000
So we have our squared right the radius squared self-teach self and then we're multiplying that by pi.

172
00:11:34,000 --> 00:11:35,220
Now we're getting an error.

173
00:11:35,310 --> 00:11:39,340
And you can tell it says self is immutable.

174
00:11:39,340 --> 00:11:46,280
And if you think back to our string extension we basically called name and then we called name that

175
00:11:46,300 --> 00:11:46,780
reverse.

176
00:11:46,780 --> 00:11:49,230
Now what we did is we took our name.

177
00:11:49,330 --> 00:11:52,870
We printed it and then we took the value of the name and we reversed it.

178
00:11:52,870 --> 00:11:55,220
Kind of making this like a temporary variable.

179
00:11:55,330 --> 00:11:57,680
We did the same thing with values not square.

180
00:11:57,880 --> 00:12:04,030
But what we want to do is we actually want to modify the value of the variable so to do that we just

181
00:12:04,030 --> 00:12:08,800
need to add a keyword at the beginning of this function mutating.

182
00:12:09,280 --> 00:12:14,560
And if you look at the little description here of mutating I don't know if it'll let me.

183
00:12:14,560 --> 00:12:14,940
It won't.

184
00:12:14,950 --> 00:12:20,590
But basically a mutating function allows that function to modify the value of whatever variable it's

185
00:12:20,590 --> 00:12:21,280
called upon.

186
00:12:21,280 --> 00:12:23,650
So let's go ahead let's try this out.

187
00:12:23,650 --> 00:12:28,090
I want to show you how it works and that to do that I'm actually going to create a quick little class

188
00:12:28,090 --> 00:12:34,690
here for a circle that's going to have a radius property so just really quickly type class circle.

189
00:12:35,260 --> 00:12:39,380
Set a variable of radius and make that a double.

190
00:12:39,550 --> 00:12:46,510
Then let's create an initializer with a property of radius of type double set self-doubt radius to be

191
00:12:46,540 --> 00:12:48,840
equal to radius.

192
00:12:48,850 --> 00:12:51,850
Sorry that was really quick but it's not super duper important.

193
00:12:51,850 --> 00:12:55,030
I just want to make sure that you saw what I did there.

194
00:12:55,150 --> 00:12:58,960
So we have our mutating function for calculate area.

195
00:12:59,050 --> 00:13:01,360
We have a class to make a circle.

196
00:13:01,390 --> 00:13:02,270
Let's make a circle.

197
00:13:02,290 --> 00:13:10,060
So type var circle WIPs circle equals circle and we're going to give it a radius value so let's think

198
00:13:10,480 --> 00:13:13,140
about three point three.

199
00:13:13,190 --> 00:13:15,420
OK so there's our radius three point three.

200
00:13:15,550 --> 00:13:22,540
And what we're able to do is we can print out a circle dot radius and watch what we get printed out

201
00:13:23,480 --> 00:13:24,190
3.3.

202
00:13:24,280 --> 00:13:25,500
Just like you would expect right.

203
00:13:25,510 --> 00:13:28,040
And let me pull this up here so you can see it.

204
00:13:28,060 --> 00:13:29,000
There we go.

205
00:13:29,470 --> 00:13:34,030
So we get three point three which is the radius we've passed then.

206
00:13:34,330 --> 00:13:42,130
So if I want to calculate the area of my circle I can call Circle dot radius and watch what I can do.

207
00:13:42,130 --> 00:13:47,620
I can call my mutating function here calculate area and you'll see in a second it'll pop up

208
00:13:51,900 --> 00:13:53,010
go calculate area.

209
00:13:53,030 --> 00:13:53,730
Cool.

210
00:13:53,820 --> 00:14:00,270
So once this runs you'll see that it prints out the area of the circle thirty four point to 1 0 9 3

211
00:14:00,270 --> 00:14:01,420
5 whatever it is.

212
00:14:01,520 --> 00:14:06,430
Now you're probably thinking how is this different at all from what we just did.

213
00:14:06,450 --> 00:14:08,520
Let me show you up here.

214
00:14:08,520 --> 00:14:15,510
When we called a square on our value of nine what it did was it created a temporary variable holding

215
00:14:15,540 --> 00:14:18,010
81 which is the square value for nine.

216
00:14:18,120 --> 00:14:25,140
But if we print out value again you'll see that the original value of the variable value is still 9

217
00:14:25,400 --> 00:14:27,420
K like that it prints out.

218
00:14:27,420 --> 00:14:33,060
Now what we can do though is with our mutating function we're actually able to change the value of the

219
00:14:33,060 --> 00:14:35,190
variable upon which this is called.

220
00:14:35,190 --> 00:14:38,130
So watch what happens if I print out circle dot radius.

221
00:14:38,130 --> 00:14:44,840
Now check out what prints out thirty four point to 1 0 9 3 5.

222
00:14:45,060 --> 00:14:47,610
That's the variable that we just changed.

223
00:14:47,640 --> 00:14:51,630
We used our mutating function to allow us to modify the variable.

224
00:14:51,630 --> 00:14:54,720
It's really cool and there are plenty of use cases for this.

225
00:14:54,720 --> 00:15:00,590
But guys this is just kind of a brief introduction to extensions some of the things you can do now in

226
00:15:00,600 --> 00:15:01,350
the next video.

227
00:15:01,350 --> 00:15:06,540
I'm actually going to show you some cool things you can do in Interface Builder in Iowa with actual

228
00:15:06,540 --> 00:15:09,720
things you might use in a real app case super duper cool.

229
00:15:09,720 --> 00:15:13,000
Thanks for watching this video guys and let's head over to part two right now.

