Created
February 2, 2018 05:05
-
-
Save nic004/10c2f85fe349dbf9a33cc2fd2fd73c30 to your computer and use it in GitHub Desktop.
Encodable struct to dictionary
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
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