Skip to content

Instantly share code, notes, and snippets.

@wibed
Created August 17, 2020 09:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wibed/7642bce4eebdb40c7be1a245fa9e7692 to your computer and use it in GitHub Desktop.
Save wibed/7642bce4eebdb40c7be1a245fa9e7692 to your computer and use it in GitHub Desktop.
/// converts any conforming struct to a Dictionary<String, String>
extension Encodable {
var asDictionary: Dictionary<String, Any>? {
let encoder = JSONEncoder()
encoder.dateEncodingStrategy = .iso8601
guard let data = try? encoder.encode(self) else { return nil }
return (try? JSONSerialization
.jsonObject(with: data, options: .allowFragments))
.flatMap { $0 as? [String: Any] }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment