Created
July 29, 2019 14:01
-
-
Save kimar/458b7a461c0912f96e439c23ea53f57b to your computer and use it in GitHub Desktop.
Swift enum unknown case default value
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
protocol UnknownCaseRepresentable: RawRepresentable, CaseIterable where RawValue: Equatable { | |
static var unknownCase: Self { get } | |
} | |
extension UnknownCaseRepresentable { | |
init(rawValue: RawValue) { | |
let value = Self.allCases.first(where: { $0.rawValue == rawValue }) | |
self = value ?? Self.unknownCase | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment