gpt4 book ai didi

ios - 具有存储枚举的条件枚举开关

转载 作者:搜寻专家 更新时间:2023-11-01 05:57:06 27 4
gpt4 key购买 nike

我希望这段代码能够工作。

我有一个枚举,其中 case Direction.Right 采用距离参数。

enum Direction {
case Up
case Down
case Left
case Right(distance: Int)
}

现在是另一个可以接受方向参数的枚举。

enum Blah {
case Move(direction: Direction)
}

let blah = Blah.Move(direction: Direction.Right(distance: 10))

当我打开 Blah 枚举时,我希望能够有条件地打开 Move.Right 就像这样...

switch blah {
case .Move(let direction) where direction == .Right:
print(direction)
default:
print("")
}

但是我得到了错误...

binary operator '==' cannot be applied to operands of type 'Direction' and '_'

有办法吗?

最佳答案

其实很简单:)

    case .Move(.Up):
print("up")
case .Move(.Right(let distance)):
print("right by", distance)

你的代码

    case .Move(let direction) where direction == .Right:

不编译,因为 == 默认只为枚举没有关联值。

关于ios - 具有存储枚举的条件枚举开关,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37835037/

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