Created
August 17, 2020 09:53
-
-
Save wibed/7642bce4eebdb40c7be1a245fa9e7692 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
/// 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