gpt4 book ai didi

swift - 如何使用单个 case 语句检查枚举值及其关联的枚举值?

转载 作者:可可西里 更新时间:2023-11-01 00:40:57 24 4
gpt4 key购买 nike

请看下面的 switch 语句。我正在寻找一种更快捷的方法来执行测试;像这样的东西:

case let .b(other) where .x = other // This does not compile

这可能吗?

enum MyEnum {
case a
case b(MyOtherEnum)
}

enum MyOtherEnum {
case x
case y
}

func check(value: MyEnum) {
switch value {
case let .b(other):
if case .x = other {
print("Got it!")
}
default:
break
}
}

最佳答案

如果您只对案例 MyEnum.b(.x) 感兴趣,没有其他cases 那么你也可以使用 ifcase 模式:

func check(value: MyEnum) {
if case .b(.x) = value {
print("Got it!")
}
}

关于swift - 如何使用单个 case 语句检查枚举值及其关联的枚举值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43746080/

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