Mercurial > public > geoquiz
diff GeoQuiz/Logic/Load.swift @ 0:413e2d21333e
first commit
author | Dennis C. M. <dennis@denniscm.com> |
---|---|
date | Tue, 20 Sep 2022 08:13:26 +0200 |
parents | |
children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/GeoQuiz/Logic/Load.swift Tue Sep 20 08:13:26 2022 +0200 @@ -0,0 +1,30 @@ +// +// Load.swift +// GeoQuiz +// +// Created by Dennis Concepción Martín on 7/9/22. +// + +import Foundation + +func load<T: Decodable>(_ filename: String) -> T { + let data: Data + + guard let file = Bundle.main.url(forResource: filename, withExtension: nil) + else { + fatalError("Couldn't find \(filename) in main bundle.") + } + + do { + data = try Data(contentsOf: file) + } catch { + fatalError("Couldn't load \(filename) from main bundle:\n\(error)") + } + + do { + let decoder = JSONDecoder() + return try decoder.decode(T.self, from: data) + } catch { + fatalError("Couldn't parse \(filename) as \(T.self):\n\(error)") + } +}