lundi 27 février 2017

Can we use `if` or `guard` instead of `switch` on an enum to extract a value?

I defined an enum and I made a computed property to extract the value associated with one of the cases. I wrote this computation with a switch:

enum NetworkResult<T> {
    case json(T)
    case error(Error)

    var error: Error? {
        switch self {
        case .error(let error):
            return error
        default:
            return nil
        }
    }
}

Is it possible to achieve the same computed property without any switch keyword? (for instance, using an if let construct?)

Aucun commentaire:

Enregistrer un commentaire