Skip to content

Instantly share code, notes, and snippets.

@aprofromindia
Last active December 12, 2019 23:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aprofromindia/4205768e7fcaca0efa14c130ec2a775f to your computer and use it in GitHub Desktop.
Save aprofromindia/4205768e7fcaca0efa14c130ec2a775f to your computer and use it in GitHub Desktop.
Swift JSONDecoder decode file
//
// Created by Apro on 06/12/19.
//
import UIKit
extension JSONDecoder {
private static let fileType = "json"
func decodeFile<T: Codable>(name: String, completion: @escaping (Result<T, Error>) -> Void) {
DispatchQueue.global(qos: .userInitiated).async { [weak self] in
let path = Bundle.main.path(forResource: name, ofType: JSONDecoder.fileType)
do {
let t = try self?.decode(T.self, from: Data(contentsOf: URL(fileURLWithPath: path!)))
DispatchQueue.main.async {
completion(.success(t!))
}
} catch {
DispatchQueue.main.async {
completion(.failure(error))
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment