Created
July 28, 2019 21:59
-
-
Save josuesilva-hotmart/f5a3c03b0ddf7d3cb0a7bf3315e994d3 to your computer and use it in GitHub Desktop.
Use Codable to decode any object like NSDictionary or Arrays
This file contains 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
extension Decodable { | |
init(_ any: Any) throws { | |
let data = try JSONSerialization.data(withJSONObject: any, options: .prettyPrinted) | |
let decoder = JSONDecoder() | |
let dateFormatter = DateFormatter() | |
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:sszzz" | |
decoder.dateDecodingStrategy = .formatted(dateFormatter) | |
self = try decoder.decode(Self.self, from: data) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment