如果情况的补充
你怎么写这个:
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 }
但我不认为它是干净的,因为它不是真正的function不能完成的情况。 另外, guard
期望我从function中返回。
任何其他的select?
你不能否定一个模式(据我所知),你使用if/else
第一个解决scheme对我来说看起来很好,代码的意图清晰可见。
switch语句将是一个替代scheme:
switch enumType { case .SomeEnum(3) where myInt == 3: break // I don't need this case default: // This is the case I need // ... }
关于你的评论
另外,警卫期望我从function中返回。
这并不完全正确。 您预计将离开当前的范围。 所以这将编译和按预期工作:
repeat { guard case .SomeEnum(3) = enumType where myInt == 3 else { // This is the case I need // ... break } } while false
但我不会认为这是一个更好的解决scheme。