Skip to content

Instantly share code, notes, and snippets.

@nic004
Created February 2, 2018 05:05
Show Gist options
  • Save nic004/10c2f85fe349dbf9a33cc2fd2fd73c30 to your computer and use it in GitHub Desktop.
Save nic004/10c2f85fe349dbf9a33cc2fd2fd73c30 to your computer and use it in GitHub Desktop.
Encodable struct to dictionary
protocol DictionaryEncodable: Encodable {}
extension DictionaryEncodable {
func dictionary() -> [String: Any]? {
let encoder = JSONEncoder()
encoder.dateEncodingStrategy = .millisecondsSince1970
guard let json = try? encoder.encode(self),
let dict = try? JSONSerialization.jsonObject(with: json, options: []) as? [String: Any] else {
return nil
}
return dict
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment