gpt4 book ai didi

ios - if 情况的补充

转载 作者:搜寻专家 更新时间:2023-10-31 22:31:55 24 4
gpt4 key购买 nike

你会怎么写:

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/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 情况的补充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37926509/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com