7

How to check the case of an enum while ignoring the associated value?

The below is what I used but it gives an error...

enum Example {
        case one(value: String)
        case two(otherValue: Int)
}

var test = Example.one(value: "A String")

if test == Example.one {   // Gives Error 
// Do Something
}

Duplicate question is overly complex.

Luke Stanyer
  • 1,404
  • 12
  • 21

1 Answers1

19

Use the below if case statement instead:

enum Example {
    case one(value: String)
    case two(otherValue: Int)
}

var test = Example.one(value: "A String")

if case Example.one(value: _) = test {   // Works
    // Do Something
}
Luke Stanyer
  • 1,404
  • 12
  • 21