Last active
December 12, 2019 23:11
-
-
Save aprofromindia/4205768e7fcaca0efa14c130ec2a775f to your computer and use it in GitHub Desktop.
Swift JSONDecoder decode file
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
// | |
// 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