Created
March 29, 2018 11:10
-
-
Save lukevanin/cfda01cc957b7328a64106095d7b7e90 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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 | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment