Created
November 11, 2017 22:55
-
-
Save pedantix/795fd39c5240f49d7d91c3a18802ab14 to your computer and use it in GitHub Desktop.
Decodable Enum
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
// 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