Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Now that we know the issues with using strings, let’s rewrite our functions using enumerations. In this video, we’ll take a look at the basic syntax of an enum as well as the concepts behind it.
-
0:00
Let's try and solve the same problem but get rid of those strings altogether and
-
0:04
use an enum.
-
0:06
To create enum, we start with the enum keyboard and
-
0:09
like any of the other custom types that we've learned about, we give it a name.
-
0:14
Following the convention we've learned about creating custom types,
-
0:18
we're also going to use upper camel case to name enums as well.
-
0:21
So, let's call this Day with an uppercase D.
-
0:25
Just as an aside, the convention is also to give enums, in both structs and
-
0:29
classes, singular name.
-
0:30
So, this will be Day, not days.
-
0:33
Now, inside the body of this enum, we define member values,
-
0:38
or members of the enumeration.
-
0:41
Each member is defined using a case keyword,
-
0:44
which is the same keyword we use inside of a switch statement.
-
0:47
So, let's add our different members.
-
0:49
So, we'll start with case sunday.
-
0:52
Here we have the member for Sunday.
-
0:54
So, this value represents Sunday.
-
0:57
Each day of the week is a different member and
-
1:00
we start each member with a case statement.
-
1:02
So, I'll say case monday.
-
1:05
Note that the convention is that the type name is upper cased but
-
1:10
all values inside the type are lowercase.
-
1:13
So, each case has a lowercase name, lower camel case.
-
1:17
So monday, tuesday, wednesday,
-
1:22
thursday, friday, and finally saturday.
-
1:28
Okay, this is our enum.
-
1:30
Pretty simple.
-
1:31
Well, what's the point of this?
-
1:33
Let's keep going.
-
1:34
So, I'm going to rewrite that function dayType.
-
1:37
So, I'll say func dayType and this is the same as we have up here.
-
1:44
So, we're going to accept a single parameter.
-
1:48
We'll give it an external name of for local name of day.
-
1:51
But unlike the last time, the argument type is not going to be string.
-
1:56
This time we want to use our new custom type, Day, the enum we just created.
-
2:01
For now, we're going to return a string from this function.
-
2:06
Inside the value of a function, we'll do something similar.
-
2:09
Let's add that switch statement again, we'll say switch on day.
-
2:14
And for the first case, we'll check if it's a Saturday or Sunday and
-
2:18
return weekend.
-
2:20
So, we'll say case Day.saturday, and then comma Day.,
-
2:24
and you'll notice something interesting.
-
2:28
The moment I put that dot and
-
2:30
start typing, Xcode starts auto completing for us.
-
2:33
There's that compiler help we were hoping for earlier.
-
2:37
Because this is an actual type we defined with a set range of values,
-
2:42
the compiler knows the different options and presents them.
-
2:46
It makes it impossible to make a mistake.
-
2:48
So Day.saturday and Day.sunday.
-
2:51
Notice how we refer to enum values.
-
2:53
We don't create instances like structs or classes.
-
2:57
Instead we simply use the type, a dot, and then it's like we're accessing a property.
-
3:02
So, Day.saturday and Day.sunday to get these values.
-
3:06
Now, if it's either one of these cases, we'll return Weekend.
-
3:11
Now, let's say I typed sarturday, like that mistake I made earlier.
-
3:17
Well, because this is an actual value and not a string, we typed value,
-
3:21
you'll notice that the compiler complains that the enum case isn't found.
-
3:25
And then suggests a fix to the right type.
-
3:29
With autocomplete, and the compiler help, and its suggested fix,
-
3:32
we've eliminated the possibility of typing an incorrect value.
-
3:36
Now, for the remaining days, well, that's a weekday, right?
-
3:39
So, let's add that in.
-
3:40
So, we'll say, case Day.monday,
-
3:45
Day.tuesday, Day.wednesday,
-
3:49
Day.thursday, and Day.friday.
-
3:53
Then we'll return Weekday.
-
3:56
Now, the body of our function is done.
-
3:59
Wait a minute.
-
4:00
Let's scroll back up and look at the previous function we wrote.
-
4:03
Earlier, over here, we had two cases, just like we do now.
-
4:06
We have "Saturday", "Sunday": return "Weekend", and
-
4:08
then the rest return "Weekday", but we also had a default statement.
-
4:12
So, what's going on here?
-
4:14
Down here we don't have a default statement.
-
4:16
I mentioned at the start of this course that enums are used to model a finite
-
4:21
data set.
-
4:21
Days of the week are a great example of this.
-
4:24
There are seven values to this data set and it will never change.
-
4:28
There will always only be seven values.
-
4:32
The set contains a fixed number of values and
-
4:35
it is this kind of data that an enum models.
-
4:38
It seems like that's a limited use case, but in fact,
-
4:41
there are many ways in which data presents itself like this.
-
4:44
Months of the year, seasons, compass directions, turn by turn navigation
-
4:49
directions, all of these are obvious examples of fixed value data sets.
-
4:54
But what about user state in an app.
-
4:56
Well, that's fixed too.
-
4:57
The user is either logged in or logged out.
-
5:00
That's two states.
-
5:01
What about different reading modes in a book app?
-
5:04
Night mode, regular, and light.
-
5:06
Typically we have three reading modes and these are fixed as well.
-
5:10
The advantage of this is that the compiler can provide some checks for you.
-
5:15
Because our day enum can only ever be seven values, by providing
-
5:19
a return value for all seven cases of the enum, the compiler knows this switch
-
5:24
statement doesn't need a default because all the possible cases are taken care of.
-
5:30
The switch statement is exhaustive,
-
5:32
that is it covers all possible paths without the need for a default value.
-
5:37
And how does it know how many paths there are?
-
5:40
We specify the type of the variable we're switching on in the argument here.
-
5:45
And since the compiler knows it's an enum, we don't have to worry about it.
-
5:50
The compiler keeps a close eye on what we're doing.
-
5:53
For example, if I remove this Day.friday case,
-
5:57
the compiler immediately knows that all paths aren't considered, and
-
6:01
complains that the switch statement must be exhaustive.
-
6:05
I can either add back the Friday case or a default case to fix this.
-
6:09
Pretty neat, right?
-
6:11
There's still a few things we can clean up in our function.
-
6:13
But let's take a break and practice what we just laerned before moving on.
You need to sign up for Treehouse in order to download course files.
Sign up