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
We got rid of some instances of strings in our function but there’s still room for improvement. In this video, we clean up both functions so there’s no room for error. After that we take a look at some of the language features that make enums easier to read and express.
-
0:00
In the last video,
-
0:01
we replaced the array if strings with an enum that was more clear and concise.
-
0:06
But our function still returns a string,
-
0:08
which is then used again in another function.
-
0:11
We're repeating the same kind of mistake here that we tried to eliminate in
-
0:14
the last video.
-
0:15
So let's try and fix that, from our dayType function here,
-
0:19
we're returning a string.
-
0:21
Let's take a look at that more closely, when it's Saturday or
-
0:24
Sunday, we return a Weekend.
-
0:27
Or if it's any other case we return Weekday,
-
0:30
I hope you've noticed the pattern here.
-
0:32
There are only two possible values that can ever be returned,
-
0:36
either a day is a weekday or it's a weekend.
-
0:40
This data set is another great candidate for an enum, so let's create another one.
-
0:44
We'll add it right after day, and this time we'll call the enum.
-
0:49
So we'll start with the enum keyword and then for the name will state dayType and
-
0:53
we'll give it two members.
-
0:55
So again we start with a case keyword to define a member, and
-
0:59
we use a lower camel case so we'll say weekday and then weekend.
-
1:05
Now if we go back to our dayType function for Saturday and
-
1:10
Sunday we can return, instead of a string dayType.
-
1:15
Oops, DayType.weekend, and for
-
1:20
the rest of the cases we can return DayType.weekday.
-
1:27
Because we're not returning strings anymore,
-
1:29
let's change that return type to DayType.
-
1:33
Now, we don't have a single string literal in this function, and
-
1:37
there's barely any room for mistakes.
-
1:39
So, now let's tackle that second function, and this one's even easier.
-
1:43
Given the day of the week we either return true, if notifications should be muted,
-
1:48
or false if not.
-
1:50
So we'll write out the same function again isNotificationMuted
-
1:57
external name of on, local name of type.
-
2:01
And now we'll specify DayType as the argument type, and return a Bool.
-
2:07
Earlier when we were talking about switch statements,
-
2:10
I said that we were only touching the tip of the proverbial iceberg.
-
2:13
And that it was capable of so much more.
-
2:16
Now we're going to start seeing that,
-
2:18
switch statements are really made to go with the enums.
-
2:21
Because the switch statement can infer the range of values, we don't have to worry
-
2:25
about default clauses, and not accounting for a particular path in our code.
-
2:30
In the body of this function, let's add another switch statement and
-
2:35
switch on the type of day.
-
2:38
So in the case of DayType.weekend,
-
2:42
we do want our notifications to be muted, so we'll return true,
-
2:49
and in the case of DayType.weekday we'll return false.
-
2:55
Let's take a look at what we've done here,
-
2:57
we took a dataset up at the top there was an array of strings.
-
3:02
And two functions that accepted strings to build some functionality.
-
3:06
This way was quite error prone, because one mistake in
-
3:10
writing out those strings and everything comes to a crashing halt.
-
3:13
We've replaced this with two custom enums, and now everything is much more elegant.
-
3:19
We can't make mistakes because X goes on to complete and
-
3:22
the compiler help automatically weed those things out.
-
3:26
This is pretty important,
-
3:27
a lot of app crashing errors are caused by using strings to specify values.
-
3:32
There are a couple things we can make a bit nicer in our code
-
3:35
syntactical sugar if you will.
-
3:38
If you're unfamiliar with that term it just means a language feature
-
3:41
that makes it easier to read or
-
3:43
express certain code, and enums certainly come with their own sugar.
-
3:48
First of all, because we've specified that the parameter day inside
-
3:52
the first function will be of type Day, we don't have to write out Day.Saturday,
-
3:57
Day.Sunday and so on.
-
3:58
We can simply emit the Day and do period .Saturday and then .Sunday.
-
4:05
And the compiler can figure it out, because the compiler knows that this case
-
4:10
is a potential option when switching on day which is of the type Day.
-
4:14
It figures out that these are valid values,
-
4:17
it figures out that .Saturday really means Day.Saturday.
-
4:20
But that's less for us to type,
-
4:22
in fact we can get rid of it everywhere inside this function.
-
4:26
So instead of saying DayType.weekend, we know that the return type is DayType,
-
4:31
so we can omit it here and infers that weekend is a valid value.
-
4:36
Let's do it everywhere else, Day.Monday becomes .Monday,
-
4:40
.Tuesday, .Wednesday, .Friday and .weekday.
-
4:45
Cool?
-
4:46
We can do you even better, we can omit the type declaration
-
4:49
in the second function for the same reason as well.
-
4:52
When we first learned about type inference and implicit versus explicit types,
-
4:57
I mentioned I would bring it up again when it was relevant.
-
5:00
Well it's quite relevant here,
-
5:02
we wrote code that's actually quite nice and readable.
-
5:05
We switch on a day and if the day Saturday or
-
5:07
Sunday we return weekend that is very readable.
-
5:10
Now the actual type though is omitting these case statements, and
-
5:14
that's a matter of judgement and style as to whether you want to write it there.
-
5:17
Now in here, let's get rid of DayType.weekend and DayType.weekday.
-
5:23
It's worth noting that we don't have to list out all the enum cases over here,
-
5:27
monday, tuesday, wednesday, thursday, friday, for the weekday option.
-
5:32
Since only two values returned a weekend and then every other value in the day enum
-
5:36
returns weekday, we can simply get rid of all this and make it the default case.
-
5:43
We also don't need to write out the word case every time, so
-
5:47
up in our enum declarations we can change it to say case weekday, weekend and
-
5:54
this is the exact same as writing every single one out on an individual line.
-
5:58
I like to keep things separate though so
-
6:00
I do like them on separate lines with individual case statements.
-
6:04
There's one thing I should mention before I end this video
-
6:07
you'll notice that I've written four functions in total.
-
6:10
There's two of them up at the top, dayType and isNotificationMuted, and
-
6:15
then two below that also say dayType and isNotificationMuted.
-
6:20
Now you may have noticed in previous lessons,
-
6:23
that sometimes we type things with the same name and we got an error.
-
6:27
So how are we doing it over here?
-
6:29
How do we have two functions named dayType and
-
6:31
two functions named isNotificationMuted?
-
6:34
This is a feature of Swift called function or
-
6:36
method overloading, where you can have two functions that have the same name
-
6:42
as long as they take different parameter types.
-
6:45
So for example DayType here, takes a string as an argument and
-
6:49
dayType here takes Day as an argument.
-
6:52
So in reality, they are two different functions,
-
6:55
even though they have the same name.
-
6:57
Now when you call the function, let's go ahead and do that.
-
6:59
And say let dayType =, and when you start typing the function name,
-
7:04
you'll notice that we have two different options with the different types listed.
-
7:09
So over here we want the one that takes a Day.
-
7:11
Now because this compiler over here, knows this parameter is of type Day,
-
7:17
the second I add a dot here it'll auto complete and show me all my options.
-
7:23
And a nice feature here is that we can see all the range of values in the enum, so
-
7:26
I can select Saturday.
-
7:28
Now I'm going to name this just type, so it's a bit more readable.
-
7:33
And then you'll see here that when we do this in the results pane weekend is shown
-
7:38
which is nice and readable again.
-
7:40
Let's finish up this example by calling our last function, so
-
7:43
we'll say let's make sure I have a different variable name.
-
7:47
So I have isMuted up here, I'm going to comment these out.
-
7:52
And then down here I'll say let isMuted = isNotificationMuted,
-
7:58
and again we have both options.
-
8:01
And we wanna take the one that takes a DayType and we can pass in the type and
-
8:05
you'll see the value is true.
You need to sign up for Treehouse in order to download course files.
Sign up