Conditional enumeration switch with saved enumeration

I want this code to work.

I have an enumeration where the Direction.Right argument takes a distance parameter.

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

Now another enumeration that can take a direction parameter.

 enum Blah { case Move(direction: Direction) } let blah = Blah.Move(direction: Direction.Right(distance: 10)) 

When I turn on the Blah enum, I want to be able to conditionally enable Move.Right, like this ...

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

But I get an error ...

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

Is there any way to do this?

+1
enums ios switch-statement swift
source share
1 answer

Actually it's pretty simple :)

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

Your code

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

does not compile, since == is defined by default only for enumeration without corresponding values.

+6
source share

All Articles