Skip to content

Instantly share code, notes, and snippets.

@mwaterfall
Created January 13, 2019 11:23
Show Gist options
  • Save mwaterfall/11f1646dd5c9a3102fdbc4f78e8d52bc to your computer and use it in GitHub Desktop.
Save mwaterfall/11f1646dd5c9a3102fdbc4f78e8d52bc to your computer and use it in GitHub Desktop.
extension RawRepresentable where RawValue: Decodable {
init(from decoder: Decoder, default: Self) throws {
let container = try decoder.singleValueContainer()
let rawValue = try container.decode(RawValue.self)
self = Self(rawValue: rawValue) ?? `default`
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment