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?

Akos Turi
Akos Turi
7,517 Points

i got en error message says "default will never be executed"

enum Day {
    case Monday
    case Tuesday
    case Wednesday
    case Thursday
    case Friday
    case Saturday
    case Sunday
}


func weekdayOrWeekend(dayOfWeek: Day) -> String {
    switch dayOfWeek {
        case Day.Monday, Day.Tuesday, Day.Wednesday, Day.Thursday, Day.Friday:
            return "It's a weekday"
        case Day.Saturday, Day.Sunday:
            return "It's the weekend"
        default:
            return "Not a valid day"
    }
}

weekdayOrWeekend(Day.Sunday)

I don't see why it doesn't wanna perform when i give an invalid value

[MOD: edited code block]

1 Answer

Hi Akos,

The code does work fine. What error value are you trying to submit?

If you send the method something that isn't a valid Day value, the method call will error rather than the default value being used. If you use a valid Day value, then the switch will pick that up accordingly.

I get a warning that the default will never be used, and that's correct; you have created a mutually exclusive set of rules; you can't submit an error into the switch; that will fail at the method call.

var: errorValue: Int
weekdayOrWeekend("Steve") // Cannot convert value of type String to expected argument type Day
weekdayOrWeekend(errorValue) // cannot convert Int to Day 

What behaviour were you expecting from this; what's not acting like you wanted it to?

Steve.