我希望这段代码能够工作。
我有一个枚举,其中 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/