Swift: Enum case not found in type

I searched a lot of questions here, I found one with a similar name. The Case Enum switch was not found in the type , but there is no solution for me.

I would like to use the enumeration with the mutation itself to solve the question of what is the next color of the traffic light in 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 put all cases as possible options and still return an error:

Enum "case" not found in type "TrafficLights"

+7
enums switch-statement swift
source share
2 answers

Cases must be declared outside 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 } } } 

Recommended: - Go through the listing - Apple Documentation

+4
source share

I had a problem with the same error when converting Int to a custom enumeration:

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

The problem is that MyEnum(rawValue: 42) returns an optional parameter. Expand it or provide an optional option to include an enumeration:

 switch MyEnum(rawValue: 42) ?? MyEnum.yourEnumDefaultCase { case .error: // no error! break default: break } 
+11
source share

All Articles