Created
February 14, 2018 04:56
-
-
Save giginet/4678c52f02f237565cb53791b1eaa949 to your computer and use it in GitHub Desktop.
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
| enum Foo: String, Codable { | |
| case a | |
| case b | |
| case c | |
| } | |
| extension RawRepresentable where RawValue == String, Self: Codable { | |
| init(from decoder: Decoder) throws { | |
| var container = try decoder.unkeyedContainer() | |
| let rawValue = try container.decode(String.self) | |
| do { | |
| self.init(rawValue: rawValue)! | |
| } | |
| } | |
| func encode(to encoder: Encoder) throws { | |
| var container = encoder.unkeyedContainer() | |
| try container.encode(rawValue) | |
| } | |
| } | |
| let encoded = try? JSONEncoder().encode(Foo.a) | |
| let decoded = try? JSONDecoder().decode(Foo.self, from: encoded!) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.