[swift-users] Checking for a specific enum case and value

Stephen Celis stephen.celis at gmail.com
Tue Dec 29 08:13:50 CST 2015


> On Dec 29, 2015, at 6:37 AM, Kristof Liliom via swift-users <swift-users at swift.org> wrote:
> 
> Hi,
> 
> What is the best way to do a one line check for an enum with a specific inner value?


I'm not sure this can be done in one line since it generally requires if- or guard-case-let.

I'd do this two-liner:

    func stoppedSuccessfully(state: State) -> Bool {
        guard case let .Stopped(val) = state else { return false }
        return val == 0
    }

Or:

    func stoppedSuccessfully(state: State) -> Bool {
        guard case let .Stopped(val) = state where val == 0 else { return false }
        return true
    }

Stephen



More information about the swift-users mailing list