Hey everybody this is Caleb with slopes dot com.
And in this video we're going to be talking about enumerations.
Enumerations are a really cool thing and swift.
It's a type that basically contains a group of related values that are all kind of under the same umbrella
as each other.
So if you think about like a you know car is the big umbrella you might have Maserati Lamborghini Geo
Metro.
Nice if you think about fruit you might have banana apple orange.
They're all related values under the same overarching type which is you know a cool way to manage it.
And basically what an enumeration allows us to do is we can work with those values in a type safe way
which is really neat.
So to show you how this works I'm actually going to go ahead and create a playground and we're going
to go ahead and I'm going to teach you the syntax of an enumeration.
I'm going to show you a couple different things you can do with enumerations and then at the very end
I'm going to show you a practical example that you could actually use in an application so you know
how you can use this.
So let's go ahead and click get started with a playground and that should pop up popped up on my other
window here.
But for now let's create a blank playground and I'm just going to name it know as teacher because I'm
the teacher.
You can name it whatever you want and go ahead and bump that open into full screen.
And we're going to go ahead.
You know what.
This might be an ex code beta but it looks like we're not going to be able to stretch that any wider
but that's fine.
So for now let's get rid of that string there we don't need it let's get rid of the boilerplate code
and let's begin.
So the syntax of an enumeration goes like this you declare it by typing inam.
OK that's short for enumeration.
And then you give it a name and think of these names like you would name a class.
OK so you're going to use capital letters at the very beginning for every word.
It will have a capital.
So just for example name of income and then you're going to give it some curly braces just like a class.
Now inside an enumeration we're going to go ahead and we're going to set up cases.
OK.
If you've ever used a switch statement you're familiar with these a switch basically allows you to go
through a bunch of different options and you call each one of those options a case.
And so for an enumeration we're going to give it cases as well so I'm just going to say Case case one
case case to case case three.
OK.
Now of course these would be named something differently in a different enumeration.
But for now this is just an example.
And as you can see the enumeration has three separate cases.
And in order to instantiate that all you need to do is to set up let in numeration and we can make it
of type.
Well actually let's do a colon there make it of type name of a name.
This of course is a terrible name but it's just for an example I'll show you some real examples in a
second.
And we can set that to be equal to one of our cases.
OK and I'll show you why this is useful in just a little bit.
Now to actually access these cases I can't just type case one.
You'll see it doesn't pop up.
But what I can do is I can type a period and that will give me access to all three of the different
cases that I've set.
So let's set this to be equal to case two.
And now you can see its value on the screen there is set to be case too.
OK that's pretty cool.
So this is the basic syntax of an enumeration you declare with ENM and you'd give it cases.
OK.
Like I said before Karr could be the name of the enim and the cases could be DeLorean Maserati Lamborghini
Cato's could be all the different cases.
Now you don't have to declare each case on a separate line.
You can actually just go ahead and use commas.
OK.
Just like when you create multiple constants or variables you can declare them in one straight line
by using commas.
So just type case one comma case two comma case three.
And as you can see that works just the same.
Ok pretty cool.
So let's go ahead and let's move on.
I'm going to show you some other cool things.
Enumerations can do.
And one of those things actually is a really great example from Apple.
So I'm going to pull open these two barcodes here and barcodes are a great example because enumerations
allow us to work with related values but they also are good for kind of differentiating different types
of things.
And so here we have a standard UPC product barcode and we also have a QR code which is used for you
know all kinds of different things storing all kinds of different information.
So there are very different kinds of barcodes but they are both barcodes.
So we're going to create an enumeration for barcode but we're going to give it two cases we'll be UPC
OK like this one here that you'd see on like the back of a box of cereal or something and then we'll
create one for the QR code.
So go ahead and type ENM barcode and we're going to give it two cases.
The first case will be case UPC.
The second will be case wups case and Q are code just like that.
Now we're going to be able to put in associated values into this barcode enumeration.
And what that's going to allow us to do is whenever we create an instance of barcode.
And let's just say that we create an instance of barcode UPC will be able to pass in values that are
associated with that and then use those later or modify them it's really cool.
So for a UPC for a product barcode you'll notice there are four different numbers.
Now at the beginning there is a number that denotes the specific number system k that a certain product
might use k.
The second set of numbers is to denote the manufacturer who makes the product.
Third is the product name of the product value basically.
These numbers are associated with a specific product and then at the very end there's what's called
a check and that basically allows for the store to know whether or not the barcode was scanned successfully.
OK.
So there are four values and what we're going to do is we're actually going to just put a parentheses
next to you PC and we can pass in four separate values in a couple which a couple is as you know a list
of values unrelated or related.
That can be either.
Now the values in this topic are going to be related and it's actually going to just be for integers
one for each of the separate number values and our barcode.
So go ahead and just type into comma int and do that four times like so.
And for the QR code.
The cool thing is a QR code can hold all kinds of information and data actually up to like two thousand
nine hundred fifty characters or something.
But what we're going to do is instead of passing it multiple values we're just going to pass in one
string value because oftentimes QR codes hold textual data K like a U R L or account information.
OK.
Very cool.
So now that we have these associated values with our numeration what we're going to be able to do is
create an instance of it and pass in those values and then we can use them later on.
So let's go ahead and let's create a barcode by typing var product barcode and we're going to set that
to be equal to Barka.
And if we push the little if we get a period there we can choose UPC or QR code.
OK let's choose a UPC for now.
And as you can see it looks a little bit like a function where passing in values.
And we're basically just setting them as for little temporary variables inside of this barcode.
So let's go ahead and let's give the values from our UPC here.
So the manufacturer is or sorry the what's it called the number system is what the manufacturer is 8
5 9 0 9 the product number is 5 1 2 2 6 and the check number is 3.
So that's cool and all but what if I wanted to print this out if I type.
Print product barcode.
Watch what happens.
It'll print down here at the bottom in just a moment so as you can see it prints out UPC and it prints
out our four values.
That's pretty cool but we can't exactly access them or pull them out in any way as you see nothing pops
up when I type a period at the end of that.
So we can't actually access it that way.
But check out what we can do.
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
to create a temporary constant and we're going to assign four constant values to the four values in
our PC then we can work with them and modify them.
So let's go ahead and let's create a switch statement for product bar code OK.
And we're going to give it to cases UPC or QR code.
So go ahead and type case and the only thing by the way since it is of type barcode The only things
that can be are a UPC or a QR code.
So we're going to go ahead and create some constants here.
So inside our switch statement here we're going to go ahead and create four constants by typing let
dot UPC.
So we're basically saying for a PC we're going to set four constants for these four values and to do
that go ahead and type parentheses and just like I said we're going to type number system.
That's the first number.
Then the manufacturer then the product then the end is a check OK.
Now we're using Let here and just like we did up here with the three cases on the same line by typing
let here and number system manufacturer product check we're creating four separate constants.
And what we're going to do is once we have those four values set we can print them out.
So print and whoops we're going to go ahead and print UPC and then using string interpolation we're
gonna go ahead and pass these four constants we've created in and see if the values get properly passed.
So inside of our special string interpolation syntax go ahead and type number system and then you know
what.
Let's actually before we type that let's copy and paste this so we don't have to type it so many times.
There we go.
So let's print number system.
OK then let's print manufacturer then product.
Whoops you know what let's call that product code.
Actually I like that better product code.
And at the end we're going to print the check value.
So watch what happens when this runs through it says that it has failed.
Now we're going to find out why that is and that's because our switch must be exhaustive.
A barcode can be UPC or QR code so we need to give it an option to do something.
If a QR code gets passed in otherwise the app could crash so it's helping us to be safe actually which
is pretty cool.
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
case.
OK let's go ahead and let's do that now for a QR code.
So case let QR code and what we're going to do is we simply just have a product code.
OK.
Because for a QR code we're only passing in one value which is a string.
So for the case product code we're going to go ahead and just print QR code and we're going to just
give it the product like so.
All right.
So now our switch is exhaustive.
It's giving us an error though let's figure out what the problem is.
We have said case let UPC number system manufacturer product code check.
Oh OK.
So the colon is in the wrong place my bet I put it at the end of the print statement.
Cool.
There we go.
That looks wonderful.
So do some cleanup here and now you can see it prints because it is a UPC barcode printed out 8 4 8
5 9 9 5 1 2 2 6 and 3.
So now we have access to those values.
Really cool.
But what happens if I wanted to change the product barcode into a QR code.
We could do that by just setting product barcode equals dot QR code and we can give it a string value.
So for that I'll just type a bunch of random characters and now that it's a QR code watch what happens
when it goes through the switch statement.
It prints it out as a QR code.
That's really cool.
So as you can see enumerations are really helpful and they can be used for a lot of different things
beyond associated values.
We can do lots of different things like we can set explicit values and types for our enumerations.
So I'm gonna show you that.
Now let me pull this back open in full screen here and let's give us some space so we can actually see
where we go.
So I'm going to create a numeration for Jet II masters and let's do that by typing ENM jet I.
M. Of course we're using the kind of conventions that you would for declaring a class with capital letters
at the beginning.
And what we're going to do is we're going to set up a few different cases so some jet masters that I
can remember right now are let's do case yota case Mace Windu.
How about Kwai Gunjan.
To end their case.
Obi Wan Kenobi.
Who else.
I'm Luke Skywalker.
I guess he could be in there.
So as you can see there are a bunch of different Jedi Masters in our enumeration and if we were to print
this out watch what happens if we print Jedi Master dot Yoda.
Watch what Prince out.
So you can see Yoda with a lowercase why Prince out.
And that's because since this is just text we haven't really told anything else.
It's actually inferring that it is a string which is cool it lets us print it out and that's really
helpful.
But what if I wanted to include Yoda's name in an app.
Of course since it's a proper noun it should have a capital letter at the beginning.
I can do that by setting an explicit type for this enumeration.
So to do that you can actually do it just like you would inherit in a class by typing a colon and adding
string.
So now each of these cases are explicitly a string.
But what we can do is we can set a value for these cases and we can access it by pulling out the raw
value.
So just like you would with a constant or variable go Hedden type Yoda K with a capital letter.
Go ahead and type Mace Windu.
Go ahead and try whoopsies.
Come on.
Go ahead and type q a gun gin.
Go ahead Hi.
Obi Wan Kenobi.
And go ahead and type Luke Skywalker without the capital K.
OK cool.
So as you can see these all now have an explicit string type which is really cool but you probably notice
that Yoda is still printing out lowercase which is the value for the case but not the explicit value
we've set to access that all we need to do is pull out the raw value.
Watch what happens.
Now when I print it prints out the capital Y Yoda.
If I were to change that to master yoda you can see it prints out Master Yoda the explicit string value
that we've set which is super cool.
So you can do this for all kinds of things.
Integers doubles even your own custom types or classes which is really neat.
And so we just printed out the raw value for the explicit type that we've set for this enumeration.
But you know what.
This is not really super practical.
And these are all just sort of toy examples.
I want to show you now how you could manage the state of a switch or a button by basically just creating
a simple enumeration to hold the status of a switch on or off.
It's simple but it's something that you could do in an app.
So go ahead and type Inam switch status.
OK and we're going to set two cases on and off which makes sense so go ahead and type case on.
And case off there's two options right now what we're going to do is we're going to go ahead and create
a variable that's going to hold the value for our or switch status and we're going to set it to be equal
to off initially and then toggle it using a function.
So let's do that now.
Go ahead and type var switch status.
Let's make it explicitly of type switch.
Oh my gosh let's make it explode.
Explicitly switch status and let's set it to be dot.
And as you can see we have our two options here that pop up.
We'll set it to be off from the beginning already.
Now we're going to write a function that is going to invert the value of our switch status if if it's
off we'll run through the function and it'll return on.
If it's on we're going to return off.
Super simple.
So go ahead and write phunk.
Whoops flip switch.
OK.
We're going to pass in a status of type switch status and we're going to return a switch status as well.
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.
Right.
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
return off just like that get really cool super simple.
So we have returned on or off depending on what status we get passed in.
Now let's go ahead remember that our switch status is currently off and what we're going to do is we're
going to go ahead and call flip switch which is our function and we're going to show how the status
of our switch is going to be flipped.
Remember it's off now and if I pass in switch status whips the one with the lowercase S..
Watch what happens when it prints out in the function over here.
Now it's on.
OK.
Really cool.
If I wanted to set our switch status to be equal to on and then if I pass it into our function now watch
what happens.
Let's pass in switch status and it's on remember.
I set it on watch what happens when I flip it it sets it to off.
So we're using an enumeration to handle on these related values and we can use enumerations to do all
kinds of cool things.
You can set associated values.
You can set explicit types and raw values for those explicit types.
You can use tuples and pass in those values to your associated types.
You can do all kinds of really really cool stuff and there's much more if you dig into the Apple documentation
on enumerations you can learn much more.
But this is just kind of to cover the basics of enumerations and all they can do to help you.
So let's go ahead and let's move over to the next video.
This is Calleigh with Debb slopes.
Scott.