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