Skip to content

Instantly share code, notes, and snippets.

@furkankaplan
Last active June 16, 2020 20:39
Show Gist options
  • Save furkankaplan/ba40693ba1fe94b8af5aba93ded2c328 to your computer and use it in GitHub Desktop.
Save furkankaplan/ba40693ba1fe94b8af5aba93ded2c328 to your computer and use it in GitHub Desktop.
//
// DictionaryExtension.swift
// reserve-ios
//
// Created by Furkan Kaplan on 16.06.2020.
// Copyright © 2020 Furkan Kaplan. All rights reserved.
//
import Foundation
extension Dictionary {
func convertTo<T2: Codable>(object class: T2.Type) -> T2? {
do {
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .iso8601
let jsonData = try JSONSerialization.data(withJSONObject: self, options: [.prettyPrinted])
let item = try decoder.decode(T2.self, from: jsonData)
print(item)
return item
} catch let error {
print(error)
}
return nil
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment