Skip to content

Instantly share code, notes, and snippets.

@atdrendel
Created August 22, 2018 09:49
Show Gist options
  • Save atdrendel/174c32cb3a6413e1094e6725e6c2a4dd to your computer and use it in GitHub Desktop.
Save atdrendel/174c32cb3a6413e1094e6725e6c2a4dd to your computer and use it in GitHub Desktop.
Encoding Dictionary<String, String>
extension Dictionary where Key==String, Value==String {
struct DictCodingKey: CodingKey {
var stringValue: String
init(stringValue: String) {
self.stringValue = stringValue
}
var intValue: Int? { return nil }
init?(intValue: Int) { return nil }
}
func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: DictCodingKey.self)
try self.forEach { (key, value) in
try container.encode(value, forKey: DictCodingKey(stringValue: key))
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment