枚举case'…'不是types'…'的成员

我有一个枚举:

enum State { case FullOpen case HalfOpen case Closed } 

和这个代码:

 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 } } 

每个switch语句都有错误:

 Enum case 'FullOpen' not found in type 'State!' Enum case 'HalfOpen' not found in type 'State!' Enum case 'Closed' not found in type 'State!' 

我不明白为什么。 有人可以解释我吗?

这样它会正常工作:

 if true { previousState = stateForConversionView switch previousState! { case .FullOpen: stateForConversionView = .HalfOpen case .HalfOpen: stateForConversionView = .FullOpen case .Closed: stateForConversionView = .HalfOpen default: break } } 

你需要添加!

欲了解更多信息,请参阅本文。

如果条件variables处于“状态”的不同types。 你应该使用rawValue属性。

 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 } 

你不需要创build一个临时variables( previousState )。 只是解开你使用枚举的属性:

 if true { switch stateForConversionView! { case .FullOpen: stateForConversionView = .HalfOpen case .HalfOpen: stateForConversionView = .FullOpen case .Closed: stateForConversionView = .HalfOpen default: break } }