ios – if案例的补充

你会怎么写这个:
if case .SomeEnum(3) = enumType where myInt == 3 {
   //I don't need this case
} else {
   //This is the case I need
}

我知道我可以使用警卫:

guard case .SomeEnum(3) = enumType where myInt == 3 else {
    //This is the case I need
}

但我不认为它是干净的,因为它不是一个功能无法完成的情况.此外,后卫希望我从该功能返回.

还有其他选择吗?

解决方法

你不能否定一种模式(据我所知)和你的第一个解决方案
使用if / else对我来说很好,代码的意图很明显
可见.

switch语句可以替代:

switch enumType {
case .SomeEnum(3) where myInt == 3:
    break  // I don't need this case
default:
    // This is the case I need
    // ...
}

关于你的评论

Also,guard expects me to return from the function.

这并不完全正确.您应该离开当前范围.
所以这将按预期编译和工作:

repeat {
    guard case .SomeEnum(3) = enumType where myInt == 3 else {
        // This is the case I need
        // ...
        break
    }
} while false

但我不认为这是一个更好的解决方案.

以上是来客网为你收集整理的ios – if案例的补充全部内容,希望文章能够帮你解决ios – if案例的补充所遇到的程序开发问题。

如果觉得来客网网站内容还不错,欢迎将来客网网站推荐给程序员好友。