具有存储枚举的条件枚举开关 [英] Conditional enum switch with stored enum
本文介绍了具有存储枚举的条件枚举开关的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我希望这段代码可以正常工作。
I'd like this code to work.
我有一个枚举,其中Direction.Right需要一个距离参数。
I have an enum where the case Direction.Right takes a distance parameter.
enum Direction {
case Up
case Down
case Left
case Right(distance: Int)
}
现在另一个可以采用Direction参数的枚举。
Now another enum that can take a Direction parameter.
enum Blah {
case Move(direction: Direction)
}
let blah = Blah.Move(direction: Direction.Right(distance: 10))
当我打开 Blah
枚举我想要有条件地打开Move.Right这样...
When I switch on the Blah
enum I want to be able to conditionally switch on the Move.Right like this...
switch blah {
case .Move(let direction) where direction == .Right:
print(direction)
default:
print("")
}
但是我收到错误...
But I get the error...
二进制运算符'=='不能应用于歌剧方向和_类型的数组
binary operator '==' cannot be applied to operands of type 'Direction' and '_'
有没有办法? / p>
Is there a way to do this?
推荐答案
其实很简单:)
case .Move(.Up):
print("up")
case .Move(.Right(let distance)):
print("right by", distance)
您的代码
case .Move(let direction) where direction == .Right:
不编译,因为 ==
仅在
枚举中定义,而没有关联值。
does not compile because ==
is defined by default only for
enumerations without associated values.
这篇关于具有存储枚举的条件枚举开关的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文