Search code examples
swiftenumsequatable

Swift Enum Equatable expected argument issue


I am using an enum as follows:

enum Views: Equatable {
    case home
    case poules(pouleID: String?)
}

I have to set an enum variable to .home or .poules. Poules can also have an argument like: router.currentView = .poules(pouleID: "TEST").

However, when I want to test if the variable is of type .home or .poules (and no not want to extract the parameter), I get the following error:

if router.currentView == .poules { // <--- (Error: Member 'poules(pouleID:)' expects argument of type 'String')
    // Do something
}

What am I doing wrong here? I have used the same structure for other Enums and have not experienced any problems there.


Solution

  • One simple way to test if a variable is of type .poules is to move the actual comparison inside a computed property of the enum. This also makes sense since this comparison isn't really what you would use for Equatable

    var isAnyPoules: Bool {
        if case .poules = self { return true }
    
        return false
    }
    

    This will make it easier to perform the check

    if router.currentView.isAnyPoules 
    

    or

    .transition(viewRouter.originView.isAnyPoules ? X : X)