Fog*_*ter 1 enums switch-statement ios swift
我希望这段代码能够运行.
我有一个枚举,其中Direction.Right的情况采用距离参数.
enum Direction {
case Up
case Down
case Left
case Right(distance: Int)
}
Run Code Online (Sandbox Code Playgroud)
现在另一个可以采用Direction参数的枚举.
enum Blah {
case Move(direction: Direction)
}
let blah = Blah.Move(direction: Direction.Right(distance: 10))
Run Code Online (Sandbox Code Playgroud)
当我打开Blah枚举时,我希望能够像这样有条件地打开Move.Right ......
switch blah {
case .Move(let direction) where direction == .Right:
print(direction)
default:
print("")
}
Run Code Online (Sandbox Code Playgroud)
但我得到错误......
二元运算符'=='不能应用于'Direction'和'_'类型的操作数
有没有办法做到这一点?
它实际上很容易:)
case .Move(.Up):
print("up")
case .Move(.Right(let distance)):
print("right by", distance)
Run Code Online (Sandbox Code Playgroud)
你的代码
case .Move(let direction) where direction == .Right:
Run Code Online (Sandbox Code Playgroud)
不编译,因为==默认情况下仅为没有关联值的枚举定义.
| 归档时间: |
|
| 查看次数: |
408 次 |
| 最近记录: |