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 Enums and Structs Enums What is an Enum?

Srinivasan Senthil
Srinivasan Senthil
2,266 Points

Simple Question on enumeration - Please help.

My Code:

enum daysinaweek                
{
    case Monday
    case Tuesday
    case Wednesday
    case Thursday
    case Friday
    case Saturday
    case Sunday
}
func checkdaysinaweek(days : daysinaweek) -> String
{
    switch days
    {
    case daysinaweek.Monday, daysinaweek.Tuesday, daysinaweek.Wednesday, daysinaweek.Thursday, daysinaweek.Friday:
        return "It is a weekday"

    case daysinaweek.Saturday, daysinaweek.Sunday:
        return "It is a weekend"

    default:
        return "This is not a valid date"
    }
}
checkdaysinaweek(daysinaweek.Saturday)
checkdaysinaweek(daysinaweek.Monday)

This code is perfectly working. I have a Questions please. Maybe i'm loosing something in basic.

  • func checkdaysinaweek(days : daysinaweek) -> String
  1. I have declared a function here.
  2. The function returns a data type of String value.
  3. It accepts a datatype which is enum type.
  4. I have declared parameter value called "days" for the argument "daysinaweek".

Q? - If i dont declare "days" the code is not working.

Something like this, where days is removed. (I just removed the word days). I feel this code is right too.

enum daysinaweek               
{
    case Monday
    case Tuesday
    case Wednesday
    case Thursday
    case Friday
    case Saturday
    case Sunday
}
func checkdaysinaweek(daysinaweek) -> String
{
    switch daysinaweek
    {
    case daysinaweek.Monday, daysinaweek.Tuesday, daysinaweek.Wednesday, daysinaweek.Thursday, daysinaweek.Friday:
        return "It is a weekday"

    case daysinaweek.Saturday, daysinaweek.Sunday:
        return "It is a weekend"

    default:
        return "This is not a valid date"
    }
}
checkdaysinaweek(daysinaweek.Saturday)
checkdaysinaweek(daysinaweek.Monday)

Note: As per my understanding, i see days as a alias or short name to call daysinaweek. I don't see a reason why it works if days is removed.

Q2: Under what circumstance will it return "Not a Valid" date. I cant see any situation, since the system forces me to use the enum type to call the function and have the argument passed?

Kindly help. Thanks.

2 Answers

Brenden Konnagan
Brenden Konnagan
16,858 Points

Hello!

Great question... Here are a couple of thoughts for you.

A1: Since you are declaring a function, Swift looks at the parameter as a constant of a type. By way of comparison look at the code below:

String = "A"

println(String)

// return error because "String" is a type not an instance of the type

We recognize that "String" is of type and needs to be named as a var or constant let.

However there is a great way to perform this function as part of the enum. Consider the following:

enum DaysInAWeek {
    case Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday

    func dayChecker() -> String {
        switch self {
        case .Monday: return "Worst Day"
        case .Tuesday: return "Yuck"
        case .Wednesday: return "Yay!"
        case .Thursday, .Friday: return "Great"
        case .Saturday, .Sunday: return "Awesome!"
        }
    }
}

let testDay = DaysInAWeek.Wednesday
testDay.dayChecker()

In this instance the function is acting on the instance already without you needing to pass in any argument.

A2: You are correct regarding your thoughts on the switch statement. As a general rule of thumb it is best to avoid using "default:" as a case in a switch statement. A switch statement requires you to be exhaustive in possible outcomes and as such one could argue that the use case of switch is exhaustive in itself. Granted there are some times when not all possible outcomes can be accounted for and thus "default:" has a place. But in this instance it is unnecessary. Simply execute a switch for every possible enum.