[swift-users] Checking for a specific enum case and value
Ryan Lovelett
swift-dev at ryan.lovelett.me
Tue Dec 29 07:30:34 CST 2015
This is what I would do.
On Tue, Dec 29, 2015, at 06:37 AM, Kristof Liliom via swift-users wrote:
> Hi,
>
> What is the best way to do a one line check for an enum with a specific
> inner value? Example:
>
> enum State {
> case Running, Stopped(Int32)
> }
>
> func stoppedSuccessfully(state: State) -> Bool {
> return state == .Stopped(0) // So only return true if state is .Stopped and the inner value is 0
> }
func stoppedSuccessfully(state: State) -> Bool {
if case let State.Stopped(value) = state where value == 0 {
return true
} else {
return false
}
}
stoppedSuccessfully(.Running) // false
stoppedSuccessfully(.Stopped(0)) // true
stoppedSuccessfully(.Stopped(100)) // false
>
> Thank You,
> Chris
>
> _______________________________________________
> swift-users mailing list
> swift-users at swift.org
> https://lists.swift.org/mailman/listinfo/swift-users
More information about the swift-users
mailing list