Created
November 4, 2020 11:49
-
-
Save josipbernat/a7f6e409d4d3182d3875acdd017a39e4 to your computer and use it in GitHub Desktop.
Compare enum with associated value by ignoring their associated values
This file contains hidden or 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
// Thanks to following StackOverflow answer: | |
// https://stackoverflow.com/questions/47597489/compare-swift-enum-types-ignoring-associated-values-generic-implementation | |
protocol ComparableCaseEnum { | |
func isSameCase(as other: Self) -> Bool | |
} | |
extension ComparableCaseEnum { | |
func isSameCase(as other: Self) -> Bool { | |
let mirrorSelf = Mirror(reflecting: self) | |
let mirrorOther = Mirror(reflecting: other) | |
if let caseSelf = mirrorSelf.children.first?.label, let caseOther = mirrorOther.children.first?.label { | |
return (caseSelf == caseOther) //Avoid nil comparation, because (nil == nil) returns true | |
} else { return false} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment