19
|
1 //
|
|
2 // FileController.swift
|
|
3 // GeoQuiz
|
|
4 //
|
|
5 // Created by Dennis Concepción Martín on 7/9/22.
|
|
6 //
|
|
7
|
|
8 import Foundation
|
|
9
|
|
10 class FileController {
|
|
11 static func load<T: Decodable>(_ filename: String) -> T {
|
|
12 let data: Data
|
|
13
|
|
14 guard let file = Bundle.main.url(forResource: filename, withExtension: nil)
|
|
15 else {
|
|
16 fatalError("Couldn't find \(filename) in main bundle.")
|
|
17 }
|
|
18
|
|
19 do {
|
|
20 data = try Data(contentsOf: file)
|
|
21 } catch {
|
|
22 fatalError("Couldn't load \(filename) from main bundle:\n\(error)")
|
|
23 }
|
|
24
|
|
25 do {
|
|
26 let decoder = JSONDecoder()
|
|
27 return try decoder.decode(T.self, from: data)
|
|
28 } catch {
|
|
29 fatalError("Couldn't parse \(filename) as \(T.self):\n\(error)")
|
|
30 }
|
|
31 }
|
|
32 }
|