The Enum case "..." is not a member of the type "..."

I have an enumeration:

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 for 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 really don't understand why. Can someone please explain to me?

+48
ios swift
Jun 27 '15 at 6:35
source share
3 answers

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 information see IT .

+114
Jun 27 '15 at 6:41
source share
β€” -

If the condition variable is in another type of "state". You must use the 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 } 
0
Sep 06 '16 at 10:05
source share

You do not need to create a temporary variable ( previousState ). Just expand the property you use as an enumeration:

 if true { switch stateForConversionView! { case .FullOpen: stateForConversionView = .HalfOpen case .HalfOpen: stateForConversionView = .FullOpen case .Closed: stateForConversionView = .HalfOpen default: break } } 
-2
May 19 '17 at 9:18 a.m.
source share



All Articles