ios 枚举 case '...' 不是类型 '...' 的成员
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31085936/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Enum case '...' is not a member of type '...'
提问by Nico
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:
我在每个 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!'
I don't really understand why. Can someone explain me please?
我真的不明白为什么。有人可以解释一下吗?
回答by Dharmesh Kheni
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.
有关更多信息,请参阅这个。
回答by Ahmed Lotfy
If the condition variable is in a different type of the "State". You should use rawValue property.
如果条件变量处于不同类型的“状态”。您应该使用 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
}
回答by Ginés SM
You don't need to create a temporary variable(previousState
). Just unwrap the property that you are using as enum:
您不需要创建临时变量( previousState
)。只需解开您用作枚举的属性:
if true {
switch stateForConversionView! {
case .FullOpen:
stateForConversionView = .HalfOpen
case .HalfOpen:
stateForConversionView = .FullOpen
case .Closed:
stateForConversionView = .HalfOpen
default:
break
}
}