Skip to content

Instantly share code, notes, and snippets.

@pedantix
Created November 11, 2017 22:55
Show Gist options
  • Save pedantix/795fd39c5240f49d7d91c3a18802ab14 to your computer and use it in GitHub Desktop.
Save pedantix/795fd39c5240f49d7d91c3a18802ab14 to your computer and use it in GitHub Desktop.
Decodable Enum
// every now and then I really find an enum to be the right construct for a data model
// in this case events have "eventTypes" and using pattern matching on the eventType is desirable
// eventTypes are based on a serverside array of possibilities "[private, public]" but theree is the possibility that
// this would be added to serverside so this should not nesecarily break the client implemenation
// to deal with this we need an unknown case
enum EventType: String, Decodable {
case `private`
case `public`
case unknown
init?(string: String) {
switch string.lowercased() {
case "public": self = .`public`
case "private": self = .`private`
default: self = .unknown
}
}
}
struct MyEvent: Decodable {
let id: String
let eventType: EventType
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment