Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Compare Enum Ignoring Associated Values? [duplicate]

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.

like image 822
Luke Stanyer Avatar asked Dec 19 '22 08:12

Luke Stanyer


1 Answers

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
}
like image 130
Luke Stanyer Avatar answered Jan 12 '23 22:01

Luke Stanyer