Swift : Enum case not found in type
Asked Answered
N

2

7

I have been searching for many questions here, I found one with similar title Enum case switch not found in type, but no solution for me.

I'd like to use enum with mutation of itself to solve question, what is the next traffic light color, at individual states.

enum TrafficLights {
    mutating func next() {
        switch self {
        case .red:
            self = .green
        case .orange:
            self = .red
        case .green:
            self = .orange
        case .none:
            self = .orange
        }
    }
}

I have put all cases as possible options and it's still returning error:

Enum 'case' not found in type 'TrafficLights'

Naevus answered 4/9, 2016 at 8:51 Comment(2)
I was confused, I thought if all cases are inside the mutating func, they do not need to be outside that func to be defined. Thanks.Naevus
I hope this isn't been used for real traffic lights...Immitigable
B
5

The cases must be declared outside of the function:

enum TrafficLights {

case green
case red
case orange
case none

mutating func next() {
    switch self {
    case .red:
        self = .green
    case .orange:
        self = .red
    case .green:
        self = .orange
    case .none:
        self = .orange
    }
  }
}

Advisable:- Go through Enumeration - Apple Documentation

Brower answered 4/9, 2016 at 8:54 Comment(3)
Thank you! My Basic fault. Now it works, so now if I remove case .none from mutating function, it would still have this case defined, even if I don't need to have next step after that state. That's what I was looking for.Naevus
Documentation:- developer.apple.com/library/mac/documentation/Swift/Conceptual/….. :)Happy codingBrower
It is also possible to add these cases in one line, so case green, red, orange, none.Bee
C
11

I was having an issue with the same error when converting an Int to a custom enum:

switch MyEnum(rawValue: 42) {
case .error:
    // Enum case `.error` not found in type 'MyEnum?'
    break
default:
    break
}

The issue is that MyEnum(rawValue: 42) returns an optional. Unwrap it or provide a non-optional to allow switching on the enum:

switch MyEnum(rawValue: 42) ?? MyEnum.yourEnumDefaultCase {
case .error:
    // no error!
    break
default:
    break
}
Costrel answered 24/11, 2016 at 8:35 Comment(1)
I'd suggest to user another way of unwrapping such as default value with ?? : (MyEnum(rawValue: 42) ?? MyEnum.error)Caricature
B
5

The cases must be declared outside of the function:

enum TrafficLights {

case green
case red
case orange
case none

mutating func next() {
    switch self {
    case .red:
        self = .green
    case .orange:
        self = .red
    case .green:
        self = .orange
    case .none:
        self = .orange
    }
  }
}

Advisable:- Go through Enumeration - Apple Documentation

Brower answered 4/9, 2016 at 8:54 Comment(3)
Thank you! My Basic fault. Now it works, so now if I remove case .none from mutating function, it would still have this case defined, even if I don't need to have next step after that state. That's what I was looking for.Naevus
Documentation:- developer.apple.com/library/mac/documentation/Swift/Conceptual/….. :)Happy codingBrower
It is also possible to add these cases in one line, so case green, red, orange, none.Bee

© 2022 - 2024 — McMap. All rights reserved.