1
00:00:08,080 --> 00:00:10,750
Hey everybody this is Caleb with slopes dot com.

2
00:00:10,750 --> 00:00:14,500
And in this video we're going to be talking about enumerations.

3
00:00:14,500 --> 00:00:18,130
Enumerations are a really cool thing and swift.

4
00:00:18,130 --> 00:00:24,400
It's a type that basically contains a group of related values that are all kind of under the same umbrella

5
00:00:24,700 --> 00:00:25,620
as each other.

6
00:00:25,870 --> 00:00:31,120
So if you think about like a you know car is the big umbrella you might have Maserati Lamborghini Geo

7
00:00:31,120 --> 00:00:32,060
Metro.

8
00:00:32,230 --> 00:00:36,630
Nice if you think about fruit you might have banana apple orange.

9
00:00:36,630 --> 00:00:43,170
They're all related values under the same overarching type which is you know a cool way to manage it.

10
00:00:43,450 --> 00:00:49,990
And basically what an enumeration allows us to do is we can work with those values in a type safe way

11
00:00:50,470 --> 00:00:51,540
which is really neat.

12
00:00:51,640 --> 00:00:56,680
So to show you how this works I'm actually going to go ahead and create a playground and we're going

13
00:00:56,680 --> 00:01:00,450
to go ahead and I'm going to teach you the syntax of an enumeration.

14
00:01:00,850 --> 00:01:04,900
I'm going to show you a couple different things you can do with enumerations and then at the very end

15
00:01:05,530 --> 00:01:10,180
I'm going to show you a practical example that you could actually use in an application so you know

16
00:01:10,180 --> 00:01:11,380
how you can use this.

17
00:01:11,380 --> 00:01:16,660
So let's go ahead and click get started with a playground and that should pop up popped up on my other

18
00:01:16,660 --> 00:01:17,470
window here.

19
00:01:17,560 --> 00:01:24,610
But for now let's create a blank playground and I'm just going to name it know as teacher because I'm

20
00:01:24,610 --> 00:01:25,210
the teacher.

21
00:01:25,210 --> 00:01:30,260
You can name it whatever you want and go ahead and bump that open into full screen.

22
00:01:30,790 --> 00:01:32,580
And we're going to go ahead.

23
00:01:32,780 --> 00:01:33,220
You know what.

24
00:01:33,220 --> 00:01:37,660
This might be an ex code beta but it looks like we're not going to be able to stretch that any wider

25
00:01:37,690 --> 00:01:38,810
but that's fine.

26
00:01:38,830 --> 00:01:44,110
So for now let's get rid of that string there we don't need it let's get rid of the boilerplate code

27
00:01:44,710 --> 00:01:46,420
and let's begin.

28
00:01:46,420 --> 00:01:52,350
So the syntax of an enumeration goes like this you declare it by typing inam.

29
00:01:52,420 --> 00:01:54,410
OK that's short for enumeration.

30
00:01:54,820 --> 00:02:00,120
And then you give it a name and think of these names like you would name a class.

31
00:02:00,130 --> 00:02:04,480
OK so you're going to use capital letters at the very beginning for every word.

32
00:02:04,480 --> 00:02:05,770
It will have a capital.

33
00:02:05,770 --> 00:02:14,040
So just for example name of income and then you're going to give it some curly braces just like a class.

34
00:02:14,080 --> 00:02:19,870
Now inside an enumeration we're going to go ahead and we're going to set up cases.

35
00:02:19,870 --> 00:02:20,420
OK.

36
00:02:20,680 --> 00:02:26,620
If you've ever used a switch statement you're familiar with these a switch basically allows you to go

37
00:02:26,620 --> 00:02:31,750
through a bunch of different options and you call each one of those options a case.

38
00:02:31,750 --> 00:02:37,930
And so for an enumeration we're going to give it cases as well so I'm just going to say Case case one

39
00:02:38,590 --> 00:02:43,200
case case to case case three.

40
00:02:43,270 --> 00:02:43,600
OK.

41
00:02:43,630 --> 00:02:47,250
Now of course these would be named something differently in a different enumeration.

42
00:02:47,260 --> 00:02:48,980
But for now this is just an example.

43
00:02:49,210 --> 00:02:53,600
And as you can see the enumeration has three separate cases.

44
00:02:53,770 --> 00:03:02,620
And in order to instantiate that all you need to do is to set up let in numeration and we can make it

45
00:03:02,680 --> 00:03:03,720
of type.

46
00:03:04,060 --> 00:03:08,230
Well actually let's do a colon there make it of type name of a name.

47
00:03:08,230 --> 00:03:12,520
This of course is a terrible name but it's just for an example I'll show you some real examples in a

48
00:03:12,520 --> 00:03:13,510
second.

49
00:03:13,510 --> 00:03:16,710
And we can set that to be equal to one of our cases.

50
00:03:16,780 --> 00:03:19,970
OK and I'll show you why this is useful in just a little bit.

51
00:03:20,110 --> 00:03:23,800
Now to actually access these cases I can't just type case one.

52
00:03:23,800 --> 00:03:25,040
You'll see it doesn't pop up.

53
00:03:25,270 --> 00:03:31,630
But what I can do is I can type a period and that will give me access to all three of the different

54
00:03:31,630 --> 00:03:32,590
cases that I've set.

55
00:03:32,590 --> 00:03:35,230
So let's set this to be equal to case two.

56
00:03:35,470 --> 00:03:39,310
And now you can see its value on the screen there is set to be case too.

57
00:03:39,560 --> 00:03:41,130
OK that's pretty cool.

58
00:03:41,140 --> 00:03:47,220
So this is the basic syntax of an enumeration you declare with ENM and you'd give it cases.

59
00:03:47,350 --> 00:03:47,940
OK.

60
00:03:48,130 --> 00:03:55,930
Like I said before Karr could be the name of the enim and the cases could be DeLorean Maserati Lamborghini

61
00:03:56,170 --> 00:03:58,120
Cato's could be all the different cases.

62
00:03:58,210 --> 00:04:01,750
Now you don't have to declare each case on a separate line.

63
00:04:01,750 --> 00:04:04,400
You can actually just go ahead and use commas.

64
00:04:04,470 --> 00:04:04,700
OK.

65
00:04:04,710 --> 00:04:10,360
Just like when you create multiple constants or variables you can declare them in one straight line

66
00:04:10,360 --> 00:04:11,320
by using commas.

67
00:04:11,440 --> 00:04:16,750
So just type case one comma case two comma case three.

68
00:04:17,260 --> 00:04:19,900
And as you can see that works just the same.

69
00:04:20,050 --> 00:04:21,520
Ok pretty cool.

70
00:04:21,520 --> 00:04:23,890
So let's go ahead and let's move on.

71
00:04:23,890 --> 00:04:25,560
I'm going to show you some other cool things.

72
00:04:25,580 --> 00:04:27,120
Enumerations can do.

73
00:04:27,460 --> 00:04:31,560
And one of those things actually is a really great example from Apple.

74
00:04:31,690 --> 00:04:37,390
So I'm going to pull open these two barcodes here and barcodes are a great example because enumerations

75
00:04:37,390 --> 00:04:44,860
allow us to work with related values but they also are good for kind of differentiating different types

76
00:04:44,890 --> 00:04:45,720
of things.

77
00:04:45,880 --> 00:04:53,160
And so here we have a standard UPC product barcode and we also have a QR code which is used for you

78
00:04:53,170 --> 00:04:56,620
know all kinds of different things storing all kinds of different information.

79
00:04:56,620 --> 00:05:01,720
So there are very different kinds of barcodes but they are both barcodes.

80
00:05:01,810 --> 00:05:08,090
So we're going to create an enumeration for barcode but we're going to give it two cases we'll be UPC

81
00:05:08,420 --> 00:05:13,250
OK like this one here that you'd see on like the back of a box of cereal or something and then we'll

82
00:05:13,250 --> 00:05:15,250
create one for the QR code.

83
00:05:15,260 --> 00:05:21,220
So go ahead and type ENM barcode and we're going to give it two cases.

84
00:05:21,290 --> 00:05:24,660
The first case will be case UPC.

85
00:05:24,740 --> 00:05:31,700
The second will be case wups case and Q are code just like that.

86
00:05:31,760 --> 00:05:38,040
Now we're going to be able to put in associated values into this barcode enumeration.

87
00:05:38,180 --> 00:05:43,260
And what that's going to allow us to do is whenever we create an instance of barcode.

88
00:05:43,400 --> 00:05:49,490
And let's just say that we create an instance of barcode UPC will be able to pass in values that are

89
00:05:49,490 --> 00:05:54,060
associated with that and then use those later or modify them it's really cool.

90
00:05:54,260 --> 00:05:59,970
So for a UPC for a product barcode you'll notice there are four different numbers.

91
00:06:00,080 --> 00:06:07,100
Now at the beginning there is a number that denotes the specific number system k that a certain product

92
00:06:07,310 --> 00:06:08,970
might use k.

93
00:06:09,140 --> 00:06:15,350
The second set of numbers is to denote the manufacturer who makes the product.

94
00:06:15,440 --> 00:06:20,340
Third is the product name of the product value basically.

95
00:06:20,360 --> 00:06:25,070
These numbers are associated with a specific product and then at the very end there's what's called

96
00:06:25,070 --> 00:06:32,110
a check and that basically allows for the store to know whether or not the barcode was scanned successfully.

97
00:06:32,240 --> 00:06:32,750
OK.

98
00:06:33,020 --> 00:06:37,670
So there are four values and what we're going to do is we're actually going to just put a parentheses

99
00:06:37,880 --> 00:06:45,740
next to you PC and we can pass in four separate values in a couple which a couple is as you know a list

100
00:06:45,890 --> 00:06:48,470
of values unrelated or related.

101
00:06:48,590 --> 00:06:49,910
That can be either.

102
00:06:50,040 --> 00:06:54,470
Now the values in this topic are going to be related and it's actually going to just be for integers

103
00:06:55,000 --> 00:06:57,890
one for each of the separate number values and our barcode.

104
00:06:57,890 --> 00:07:03,860
So go ahead and just type into comma int and do that four times like so.

105
00:07:03,860 --> 00:07:05,210
And for the QR code.

106
00:07:05,300 --> 00:07:10,880
The cool thing is a QR code can hold all kinds of information and data actually up to like two thousand

107
00:07:10,880 --> 00:07:13,550
nine hundred fifty characters or something.

108
00:07:13,550 --> 00:07:17,570
But what we're going to do is instead of passing it multiple values we're just going to pass in one

109
00:07:17,570 --> 00:07:25,160
string value because oftentimes QR codes hold textual data K like a U R L or account information.

110
00:07:25,470 --> 00:07:25,790
OK.

111
00:07:25,790 --> 00:07:26,630
Very cool.

112
00:07:26,630 --> 00:07:32,600
So now that we have these associated values with our numeration what we're going to be able to do is

113
00:07:32,600 --> 00:07:36,680
create an instance of it and pass in those values and then we can use them later on.

114
00:07:36,680 --> 00:07:43,340
So let's go ahead and let's create a barcode by typing var product barcode and we're going to set that

115
00:07:43,340 --> 00:07:44,910
to be equal to Barka.

116
00:07:45,440 --> 00:07:51,460
And if we push the little if we get a period there we can choose UPC or QR code.

117
00:07:51,530 --> 00:07:53,590
OK let's choose a UPC for now.

118
00:07:53,870 --> 00:07:58,090
And as you can see it looks a little bit like a function where passing in values.

119
00:07:58,100 --> 00:08:04,190
And we're basically just setting them as for little temporary variables inside of this barcode.

120
00:08:04,190 --> 00:08:08,420
So let's go ahead and let's give the values from our UPC here.

121
00:08:08,420 --> 00:08:14,990
So the manufacturer is or sorry the what's it called the number system is what the manufacturer is 8

122
00:08:15,050 --> 00:08:21,720
5 9 0 9 the product number is 5 1 2 2 6 and the check number is 3.

123
00:08:21,890 --> 00:08:25,460
So that's cool and all but what if I wanted to print this out if I type.

124
00:08:25,460 --> 00:08:27,520
Print product barcode.

125
00:08:27,560 --> 00:08:28,540
Watch what happens.

126
00:08:28,560 --> 00:08:36,190
It'll print down here at the bottom in just a moment so as you can see it prints out UPC and it prints

127
00:08:36,190 --> 00:08:37,210
out our four values.

128
00:08:37,330 --> 00:08:42,490
That's pretty cool but we can't exactly access them or pull them out in any way as you see nothing pops

129
00:08:42,490 --> 00:08:44,660
up when I type a period at the end of that.

130
00:08:44,740 --> 00:08:47,380
So we can't actually access it that way.

131
00:08:47,770 --> 00:08:49,850
But check out what we can do.

132
00:08:49,960 --> 00:08:55,930
We can create a switch statement and we're going to pass our bar code in and if it is a PC we're going

133
00:08:55,930 --> 00:09:01,630
to create a temporary constant and we're going to assign four constant values to the four values in

134
00:09:01,630 --> 00:09:04,540
our PC then we can work with them and modify them.

135
00:09:04,540 --> 00:09:09,980
So let's go ahead and let's create a switch statement for product bar code OK.

136
00:09:10,060 --> 00:09:13,570
And we're going to give it to cases UPC or QR code.

137
00:09:13,780 --> 00:09:19,520
So go ahead and type case and the only thing by the way since it is of type barcode The only things

138
00:09:19,520 --> 00:09:23,390
that can be are a UPC or a QR code.

139
00:09:23,590 --> 00:09:27,520
So we're going to go ahead and create some constants here.

140
00:09:27,520 --> 00:09:32,170
So inside our switch statement here we're going to go ahead and create four constants by typing let

141
00:09:32,630 --> 00:09:33,990
dot UPC.

142
00:09:34,090 --> 00:09:40,120
So we're basically saying for a PC we're going to set four constants for these four values and to do

143
00:09:40,120 --> 00:09:46,990
that go ahead and type parentheses and just like I said we're going to type number system.

144
00:09:46,990 --> 00:09:48,170
That's the first number.

145
00:09:48,460 --> 00:09:56,430
Then the manufacturer then the product then the end is a check OK.

146
00:09:56,710 --> 00:10:02,860
Now we're using Let here and just like we did up here with the three cases on the same line by typing

147
00:10:02,860 --> 00:10:07,970
let here and number system manufacturer product check we're creating four separate constants.

148
00:10:08,170 --> 00:10:12,230
And what we're going to do is once we have those four values set we can print them out.

149
00:10:12,250 --> 00:10:22,330
So print and whoops we're going to go ahead and print UPC and then using string interpolation we're

150
00:10:22,380 --> 00:10:29,110
gonna go ahead and pass these four constants we've created in and see if the values get properly passed.

151
00:10:29,110 --> 00:10:35,080
So inside of our special string interpolation syntax go ahead and type number system and then you know

152
00:10:35,080 --> 00:10:35,220
what.

153
00:10:35,230 --> 00:10:40,870
Let's actually before we type that let's copy and paste this so we don't have to type it so many times.

154
00:10:40,870 --> 00:10:41,330
There we go.

155
00:10:41,350 --> 00:10:43,260
So let's print number system.

156
00:10:43,630 --> 00:10:48,600
OK then let's print manufacturer then product.

157
00:10:48,920 --> 00:10:52,510
Whoops you know what let's call that product code.

158
00:10:52,510 --> 00:10:56,200
Actually I like that better product code.

159
00:10:56,260 --> 00:10:58,450
And at the end we're going to print the check value.

160
00:10:58,510 --> 00:11:03,700
So watch what happens when this runs through it says that it has failed.

161
00:11:03,700 --> 00:11:08,530
Now we're going to find out why that is and that's because our switch must be exhaustive.

162
00:11:08,650 --> 00:11:13,890
A barcode can be UPC or QR code so we need to give it an option to do something.

163
00:11:13,900 --> 00:11:19,630
If a QR code gets passed in otherwise the app could crash so it's helping us to be safe actually which

164
00:11:19,630 --> 00:11:20,640
is pretty cool.

165
00:11:20,980 --> 00:11:27,130
Also at the end of a case for a switch we need to put a colon so it knows that that's the end of that

166
00:11:27,130 --> 00:11:27,880
case.

167
00:11:27,990 --> 00:11:31,930
OK let's go ahead and let's do that now for a QR code.

168
00:11:31,960 --> 00:11:38,910
So case let QR code and what we're going to do is we simply just have a product code.

169
00:11:39,170 --> 00:11:39,600
OK.

170
00:11:39,640 --> 00:11:44,870
Because for a QR code we're only passing in one value which is a string.

171
00:11:44,870 --> 00:11:53,320
So for the case product code we're going to go ahead and just print QR code and we're going to just

172
00:11:53,320 --> 00:11:56,570
give it the product like so.

173
00:11:56,980 --> 00:11:57,560
All right.

174
00:11:57,730 --> 00:12:00,250
So now our switch is exhaustive.

175
00:12:00,340 --> 00:12:05,010
It's giving us an error though let's figure out what the problem is.

176
00:12:05,080 --> 00:12:10,130
We have said case let UPC number system manufacturer product code check.

177
00:12:10,300 --> 00:12:10,770
Oh OK.

178
00:12:10,780 --> 00:12:14,540
So the colon is in the wrong place my bet I put it at the end of the print statement.

179
00:12:14,710 --> 00:12:15,050
Cool.

180
00:12:15,070 --> 00:12:15,820
There we go.

181
00:12:15,820 --> 00:12:17,390
That looks wonderful.

182
00:12:17,430 --> 00:12:24,850
So do some cleanup here and now you can see it prints because it is a UPC barcode printed out 8 4 8

183
00:12:24,850 --> 00:12:27,100
5 9 9 5 1 2 2 6 and 3.

184
00:12:27,100 --> 00:12:29,910
So now we have access to those values.

185
00:12:30,040 --> 00:12:30,860
Really cool.

186
00:12:31,000 --> 00:12:36,470
But what happens if I wanted to change the product barcode into a QR code.

187
00:12:36,760 --> 00:12:44,470
We could do that by just setting product barcode equals dot QR code and we can give it a string value.

188
00:12:44,470 --> 00:12:48,730
So for that I'll just type a bunch of random characters and now that it's a QR code watch what happens

189
00:12:48,730 --> 00:12:50,320
when it goes through the switch statement.

190
00:12:50,560 --> 00:12:52,550
It prints it out as a QR code.

191
00:12:52,570 --> 00:12:53,290
That's really cool.

192
00:12:53,290 --> 00:12:59,080
So as you can see enumerations are really helpful and they can be used for a lot of different things

193
00:12:59,800 --> 00:13:01,630
beyond associated values.

194
00:13:01,630 --> 00:13:07,050
We can do lots of different things like we can set explicit values and types for our enumerations.

195
00:13:07,050 --> 00:13:07,990
So I'm gonna show you that.

196
00:13:07,990 --> 00:13:13,480
Now let me pull this back open in full screen here and let's give us some space so we can actually see

197
00:13:14,140 --> 00:13:15,460
where we go.

198
00:13:15,760 --> 00:13:24,290
So I'm going to create a numeration for Jet II masters and let's do that by typing ENM jet I.

199
00:13:24,350 --> 00:13:30,370
M. Of course we're using the kind of conventions that you would for declaring a class with capital letters

200
00:13:30,370 --> 00:13:31,340
at the beginning.

201
00:13:31,450 --> 00:13:36,040
And what we're going to do is we're going to set up a few different cases so some jet masters that I

202
00:13:36,040 --> 00:13:43,530
can remember right now are let's do case yota case Mace Windu.

203
00:13:43,870 --> 00:13:47,760
How about Kwai Gunjan.

204
00:13:47,950 --> 00:13:50,240
To end their case.

205
00:13:50,290 --> 00:13:52,840
Obi Wan Kenobi.

206
00:13:52,870 --> 00:13:53,730
Who else.

207
00:13:53,790 --> 00:13:56,260
I'm Luke Skywalker.

208
00:13:56,290 --> 00:13:58,500
I guess he could be in there.

209
00:13:58,570 --> 00:14:06,280
So as you can see there are a bunch of different Jedi Masters in our enumeration and if we were to print

210
00:14:06,280 --> 00:14:12,670
this out watch what happens if we print Jedi Master dot Yoda.

211
00:14:12,700 --> 00:14:15,660
Watch what Prince out.

212
00:14:15,690 --> 00:14:19,080
So you can see Yoda with a lowercase why Prince out.

213
00:14:19,320 --> 00:14:23,320
And that's because since this is just text we haven't really told anything else.

214
00:14:23,400 --> 00:14:28,530
It's actually inferring that it is a string which is cool it lets us print it out and that's really

215
00:14:28,530 --> 00:14:29,400
helpful.

216
00:14:29,400 --> 00:14:33,160
But what if I wanted to include Yoda's name in an app.

217
00:14:33,180 --> 00:14:37,090
Of course since it's a proper noun it should have a capital letter at the beginning.

218
00:14:37,200 --> 00:14:40,470
I can do that by setting an explicit type for this enumeration.

219
00:14:40,530 --> 00:14:47,040
So to do that you can actually do it just like you would inherit in a class by typing a colon and adding

220
00:14:47,040 --> 00:14:47,920
string.

221
00:14:48,270 --> 00:14:51,980
So now each of these cases are explicitly a string.

222
00:14:51,990 --> 00:14:57,660
But what we can do is we can set a value for these cases and we can access it by pulling out the raw

223
00:14:57,660 --> 00:14:58,420
value.

224
00:14:58,440 --> 00:15:06,330
So just like you would with a constant or variable go Hedden type Yoda K with a capital letter.

225
00:15:06,780 --> 00:15:10,490
Go ahead and type Mace Windu.

226
00:15:10,780 --> 00:15:13,350
Go ahead and try whoopsies.

227
00:15:13,500 --> 00:15:14,140
Come on.

228
00:15:14,370 --> 00:15:21,340
Go ahead and type q a gun gin.

229
00:15:21,840 --> 00:15:23,180
Go ahead Hi.

230
00:15:23,220 --> 00:15:26,190
Obi Wan Kenobi.

231
00:15:26,910 --> 00:15:32,720
And go ahead and type Luke Skywalker without the capital K.

232
00:15:33,060 --> 00:15:33,690
OK cool.

233
00:15:33,690 --> 00:15:39,420
So as you can see these all now have an explicit string type which is really cool but you probably notice

234
00:15:39,420 --> 00:15:44,970
that Yoda is still printing out lowercase which is the value for the case but not the explicit value

235
00:15:44,970 --> 00:15:49,890
we've set to access that all we need to do is pull out the raw value.

236
00:15:49,890 --> 00:15:50,490
Watch what happens.

237
00:15:50,490 --> 00:15:53,430
Now when I print it prints out the capital Y Yoda.

238
00:15:53,430 --> 00:16:01,200
If I were to change that to master yoda you can see it prints out Master Yoda the explicit string value

239
00:16:01,200 --> 00:16:03,230
that we've set which is super cool.

240
00:16:03,420 --> 00:16:05,550
So you can do this for all kinds of things.

241
00:16:05,550 --> 00:16:10,860
Integers doubles even your own custom types or classes which is really neat.

242
00:16:10,980 --> 00:16:16,980
And so we just printed out the raw value for the explicit type that we've set for this enumeration.

243
00:16:16,980 --> 00:16:17,620
But you know what.

244
00:16:17,630 --> 00:16:19,840
This is not really super practical.

245
00:16:19,850 --> 00:16:22,320
And these are all just sort of toy examples.

246
00:16:22,320 --> 00:16:28,470
I want to show you now how you could manage the state of a switch or a button by basically just creating

247
00:16:28,470 --> 00:16:33,380
a simple enumeration to hold the status of a switch on or off.

248
00:16:33,390 --> 00:16:36,420
It's simple but it's something that you could do in an app.

249
00:16:36,420 --> 00:16:40,720
So go ahead and type Inam switch status.

250
00:16:41,050 --> 00:16:47,390
OK and we're going to set two cases on and off which makes sense so go ahead and type case on.

251
00:16:47,730 --> 00:16:54,060
And case off there's two options right now what we're going to do is we're going to go ahead and create

252
00:16:54,060 --> 00:17:00,120
a variable that's going to hold the value for our or switch status and we're going to set it to be equal

253
00:17:00,120 --> 00:17:04,140
to off initially and then toggle it using a function.

254
00:17:04,140 --> 00:17:05,390
So let's do that now.

255
00:17:05,600 --> 00:17:08,500
Go ahead and type var switch status.

256
00:17:08,910 --> 00:17:12,860
Let's make it explicitly of type switch.

257
00:17:12,930 --> 00:17:15,400
Oh my gosh let's make it explode.

258
00:17:15,720 --> 00:17:20,660
Explicitly switch status and let's set it to be dot.

259
00:17:21,030 --> 00:17:23,780
And as you can see we have our two options here that pop up.

260
00:17:23,820 --> 00:17:27,140
We'll set it to be off from the beginning already.

261
00:17:27,280 --> 00:17:33,930
Now we're going to write a function that is going to invert the value of our switch status if if it's

262
00:17:34,020 --> 00:17:36,990
off we'll run through the function and it'll return on.

263
00:17:37,200 --> 00:17:39,420
If it's on we're going to return off.

264
00:17:39,420 --> 00:17:40,100
Super simple.

265
00:17:40,110 --> 00:17:41,840
So go ahead and write phunk.

266
00:17:42,160 --> 00:17:44,370
Whoops flip switch.

267
00:17:44,850 --> 00:17:45,510
OK.

268
00:17:45,510 --> 00:17:52,340
We're going to pass in a status of type switch status and we're going to return a switch status as well.

269
00:17:52,650 --> 00:18:03,860
OK so if we pass in a status of off if status is equal to off we're going to go ahead and return on.

270
00:18:03,930 --> 00:18:04,170
Right.

271
00:18:04,170 --> 00:18:09,870
That makes sense if it's off we're going to turn it on else meaning if we pass it on we're going to

272
00:18:09,870 --> 00:18:15,250
return off just like that get really cool super simple.

273
00:18:15,540 --> 00:18:20,160
So we have returned on or off depending on what status we get passed in.

274
00:18:20,160 --> 00:18:25,980
Now let's go ahead remember that our switch status is currently off and what we're going to do is we're

275
00:18:25,980 --> 00:18:30,960
going to go ahead and call flip switch which is our function and we're going to show how the status

276
00:18:30,960 --> 00:18:32,990
of our switch is going to be flipped.

277
00:18:33,030 --> 00:18:38,710
Remember it's off now and if I pass in switch status whips the one with the lowercase S..

278
00:18:38,790 --> 00:18:42,400
Watch what happens when it prints out in the function over here.

279
00:18:42,420 --> 00:18:43,300
Now it's on.

280
00:18:43,400 --> 00:18:43,600
OK.

281
00:18:43,600 --> 00:18:44,790
Really cool.

282
00:18:45,150 --> 00:18:52,680
If I wanted to set our switch status to be equal to on and then if I pass it into our function now watch

283
00:18:52,680 --> 00:18:53,480
what happens.

284
00:18:53,670 --> 00:18:56,960
Let's pass in switch status and it's on remember.

285
00:18:57,060 --> 00:19:01,190
I set it on watch what happens when I flip it it sets it to off.

286
00:19:01,200 --> 00:19:08,250
So we're using an enumeration to handle on these related values and we can use enumerations to do all

287
00:19:08,250 --> 00:19:09,430
kinds of cool things.

288
00:19:09,450 --> 00:19:11,120
You can set associated values.

289
00:19:11,130 --> 00:19:15,100
You can set explicit types and raw values for those explicit types.

290
00:19:15,240 --> 00:19:21,150
You can use tuples and pass in those values to your associated types.

291
00:19:21,150 --> 00:19:26,100
You can do all kinds of really really cool stuff and there's much more if you dig into the Apple documentation

292
00:19:26,400 --> 00:19:28,080
on enumerations you can learn much more.

293
00:19:28,080 --> 00:19:32,320
But this is just kind of to cover the basics of enumerations and all they can do to help you.

294
00:19:32,340 --> 00:19:34,800
So let's go ahead and let's move over to the next video.

295
00:19:34,800 --> 00:19:36,470
This is Calleigh with Debb slopes.

296
00:19:36,490 --> 00:19:36,850
Scott.
