Gintas_
Gintas_

Reputation: 5030

Swift - how to obtain enum case argument from enum variable?

enum ApiError: Swift.Error
{
    case SimpleErrorMessage(message: String)
}


// Observable ...
.map({ (response, json) -> Bool in
            if(response.statusCode == 200)
            {
                return true
            }
            else if(response.statusCode == 404)
            {
                return false
            }
            else
            {
                throw ApiError.SimpleErrorMessage(message: "test")
            }
        })

// Observable
.subscribe(onError: { [weak self] (error) in
                // todo notify somehow?
                guard let err = error as? ApiError else { return }
                if case .SimpleErrorMessage == error
                {
                }
        })

How to obtain message variable in onError?

Upvotes: 0

Views: 129

Answers (1)

CZ54
CZ54

Reputation: 5586

Use can use :

if case .SimpleErrorMessage(message: let message) == error

Generally you can use :

switch error {
    case .SimpleErrorMessage(message: let message):
        //Use the message
       break
    }

NB: In swift 4, your enum case should be lowercased : SimpleErrorMessage -> simpleErrorMessage

Upvotes: 2

Related Questions