【发布时间】:2016-06-20 15:39:27
【问题描述】:
你会怎么写:
if case .SomeEnum(3) = enumType where myInt == 3 {
//I don't need this case
} else {
//This is the case I need
}
我知道我可以使用guard:
guard case .SomeEnum(3) = enumType where myInt == 3 else {
//This is the case I need
}
但我不认为它是干净的,因为这并不是函数无法完成的情况。另外,guard 期望我从函数中返回。
还有其他选择吗?
【问题讨论】:
-
不就是
if .SomeEnum != enumType || myInt != 3吗? -
.SomeEnum != enumType或.SomeEnum == enumType给出编译器错误,因为枚举有参数 -
哦,我明白了。好问题:)
-
据我所知,您不能否定一个模式。我认为您的第一个解决方案是最好的解决方案,或者一个带有一个 case 加上
defaultcase 的 switch 语句。 – 你可以在本地范围内使用守卫,但这更像是混淆 Swift。