Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Swift 2.0 Enumerations and Optionals Introduction to Enumerations Getting Rid of Strings

Micah DeLaurentis
Micah DeLaurentis
3,534 Points

fixed data, so why list all cases

Since there are only 7 values, why do you have to list all the cases? Why not just sat, sun = weekend, and some key word like "else" or "otherwise"-- weekday.

2 Answers

faraz
PLUS
faraz
Courses Plus Student 21,474 Points

You can do that. See the following example:

func weekdayOrWeekend(day: Day) -> String {
    switch day {
    case .Saturday, .Sunday: return "Weekend"
    default: return "Weekday"
    }
}

and call it like this:

weekdayOrWeekend(.Monday) // Returns Weekday
weekdayOrWeekend(.Saturday) // Returns Weekend

In the code above, only Saturday and Sunday would return Weekend, and all other Day values passed in would return Weekday. Hope that helps illustrate the point! Let me know if you have another other questions.

Cindy Lea
PLUS
Cindy Lea
Courses Plus Student 6,497 Points

You can, but they probably are showing you the different cases so you can see how it works with more cases. Once you understand the concept, you can always simplify.