Skip to content

Instantly share code, notes, and snippets.

@lukevanin
Created March 29, 2018 11:10
Show Gist options
  • Select an option

  • Save lukevanin/cfda01cc957b7328a64106095d7b7e90 to your computer and use it in GitHub Desktop.

Select an option

Save lukevanin/cfda01cc957b7328a64106095d7b7e90 to your computer and use it in GitHub Desktop.

Revisions

  1. lukevanin created this gist Mar 29, 2018.
    42 changes: 42 additions & 0 deletions complex_enum_equatable.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,42 @@
    //
    // Example showing how to add Equatable protocol conformance to a complex enum using a switch statement.
    //
    // Useful if you need to compare two enums, e.g:
    // let a = ApplicationViewState.content("foo")
    // let b = ApplicationViewState.connectionError
    // let c = ApplicationViewState.content("foo"
    // a == b // false
    //

    enum ApplicationViewState {
    case sync
    case content(String)
    case login
    case connectionError
    case versionError(URL)
    }

    extension ApplicationViewState: Equatable {
    static func ==(lhs: ApplicationViewState, rhs: ApplicationViewState) -> Bool {
    switch (lhs, rhs) {

    case (.sync, .sync):
    return true

    case (.content(let lhs), .content(let rhs)):
    return lhs == rhs

    case (.login, .login):
    return true

    case (.connectionError, .connectionError):
    return true

    case (.versionError(let lhs), .versionError(let rhs)):
    return lhs == rhs

    default:
    return false
    }
    }
    }