How to compare enum with associated values by ignoring its associated value in Swift?

Edit: As Etan points out, you can omit the (_) wildcard match to use this more cleanly.


Unfortunately, I don’t believe that there’s an easier way than your switch approach in Swift 1.2.

In Swift 2, however, you can use the new if-case pattern match:

let number = CardRank.Number(5)
if case .Number(_) = number {
    // Is a number
} else {
    // Something else
}

If you’re looking to avoid verbosity, you might consider adding an isNumber computed property to your enum that implements your switch statement.

Leave a Comment