Skip to content

Matching Cases

Giuseppe Lanza edited this page Aug 22, 2019 · 1 revision

EnumKit provides utilities functions for case matching.

Having a CaseAccessible enum

enum MyError: CaseAccessible, Error {
    case notFound(String)
    case noConnection(String)
    case failedWithMessage(String)
}

matching a case pattern can be achieved with matches(case:) function

let isConnectionIssue = myError.matches(case: MyError.noConnection) // Bool

or with ~= swift pattern operator

let isConnectionIssue = myError ~= MyError.noConnection // Bool

similarly negations are possible with the isNotMatching(case:) function or with the !~= pattern operator

// is not matching

let isNotConnectionIssue = myError.isNotMatching(case: MyError.noConnection) // Bool
let isNotConnectionIssue = myError !~= MyError.noConnection // Bool

Clone this wiki locally