Skip to content

Instantly share code, notes, and snippets.

@kimar
Created July 29, 2019 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kimar/458b7a461c0912f96e439c23ea53f57b to your computer and use it in GitHub Desktop.
Save kimar/458b7a461c0912f96e439c23ea53f57b to your computer and use it in GitHub Desktop.
Swift enum unknown case default value
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