Skip to content

Instantly share code, notes, and snippets.

@josuesilva-hotmart
Created July 28, 2019 21:59
Show Gist options
  • Save josuesilva-hotmart/f5a3c03b0ddf7d3cb0a7bf3315e994d3 to your computer and use it in GitHub Desktop.
Save josuesilva-hotmart/f5a3c03b0ddf7d3cb0a7bf3315e994d3 to your computer and use it in GitHub Desktop.
Use Codable to decode any object like NSDictionary or Arrays
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