Sometimes when you're coding, you want to select from a list of values, you want to assign a list
of values to things that you're pulling from a server.
Now, the naive way to do that is to assign some strings.
So if you have music, you'll have music if you have podcast podcasts.
But that's not the way we do things.
What we need to use for that is called an enumeration enum for short.
A good example of this is a compass point.
So this is how we define an enum and the and enum takes cases.
So we have a case for North Whips, you know why it's done that?
We have a case for South.
And we have a case.
For West, the case for east.
Right, so now we have an enemy.
How do we use it?
Well, let's say we have some kind of variable and we want to set it to one of these options will say
for a equals.
Compass points.
Don't know if.
And that is it, the reason we do this, and you may think it's a bit weird that we do this if you're
just learning coding, but the reason we do it is so we have a central location like we should with
all code where our values are stored and there are a finite number of options.
So if I come along to Compass Point North, I know what I'm talking about.
And if I wanted to change that.
I could do the following, and that should change it everywhere else if we refactor the whole thing.
So we have a single central location where we define these things.
Now, the other way we can define an enum is to have.
That's a compass point out.
Yes, curly brace, thank you, and we will have case.
North, south.
East, west, and we just put commas between them, so I haven't typed out the whole thing here.
You don't want to go through a typing class de.
OK, so that's the other way we can define this.
We don't have to type each one out just nice and compact, depending on how many cases you have, of
course.
Right.
But I would say, you know, with code, don't try and save lines unnecessarily.
I actually prefer this first version because it makes it really easy to read.
So that's that.
So what else have I got here?
I've got this Inam, which comes from the Swift Example's website, the Aski Control Character, which
is, of course a character type so we can assign values to our enemies.
So when I have a case of tab, I can have backslash t line feeds backslash in which you've seen in the
print statement and the return is backslash.
Are all of these things on all OSes will generally go on to the next line line feed and carriage return
tab will advance forward by a set number of spaces.
OK, so that's how we assign variables into things.
Now, if I was going to use that some way, I would have something like this to tap.
Now I know wherever I use that, I'm getting this character.
So in the future, if that character changes to forward slash something, I change it in one place and
it gets carried through everywhere.
Forward slash is a bad idea.
OK.
Oh, because I've got characters.
Yeah, if it was a string, then I could change it.
But if the character thing changes from T to P, then that gets changed everywhere.
So I've only got one place to keep it.
And in fact, this is a terrible example because it keeps giving me errors because the backslash t is
a specific ASCII character.
But anyway, let's let's leave that aside for one minute.
Let's pretend that I actually got it right.
The point is, if I change it once here, it changes throughout the application.
And that's the way you should always construct your code.
Right.
So here's a bit of homework for you.
It's really easy.
We're making a music map.
Our server returns a list of music, podcasts or albums, make an enum to represent those.
Nice and simple.