Fogmeister Fogmeister - 11 months ago 31
iOS Question

Conditional enum switch with stored enum

I'd like this code to work.

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)

Now another enum that can take a Direction parameter.

enum Blah {
case Move(direction: Direction)

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

When I switch on the
enum I want to be able to conditionally switch on the Move.Right like this...

switch blah {
case .Move(let direction) where direction == .Right:

But I get the error...

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

Is there a way to do this?


It is actually quite easy :)

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

Your code

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

does not compile because == is defined by default only for enumerations without associated values.