I have an enum:
enum State {
case FullOpen
case HalfOpen
case Closed
}
and this code:
var stateForConversionView: State!
...
var previousState: State!
if true {
previousState = stateForConversionView!
switch previousState {
case .FullOpen:
stateForConversionView = .HalfOpen
case .HalfOpen:
stateForConversionView = .FullOpen
case .Closed:
stateForConversionView = .HalfOpen
default:
break
}
}
I got an error on each switch statement:
Enum case 'FullOpen' not found in type 'State!'
Enum case 'HalfOpen' not found in type 'State!'
Enum case 'Closed' not found in type 'State!'
I don't really understand why. Can someone explain me please?
This way It will work fine :
if true {
previousState = stateForConversionView
switch previousState! {
case .FullOpen:
stateForConversionView = .HalfOpen
case .HalfOpen:
stateForConversionView = .FullOpen
case .Closed:
stateForConversionView = .HalfOpen
default:
break
}
}
You need to add !
.
For more info refer THIS.
If the condition variable is in a different type of the "State". You should use rawValue property.
var previousState:String
previousState = stateForConversionView
switch previousState {
case State.FullOpen.rawValue:
stateForConversionView = .HalfOpen
case State.HalfOpen.rawValue:
stateForConversionView = .FullOpen
case State.Closed.rawValue:
stateForConversionView = .HalfOpen
default:break
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With