Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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!)
Add Comment
Please, Sign In to add comment