Mercurial > public > simoleon
comparison Simoleon/Functions/ParseJson.swift @ 0:e0c2bda6c51f
first commit
author | Dennis Concepción Martín <dennisconcepcionmartin@gmail.com> |
---|---|
date | Sun, 11 Jul 2021 15:13:29 +0100 |
parents | |
children | 75c1a05176f6 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:e0c2bda6c51f |
---|---|
1 // | |
2 // ParseJson.swift | |
3 // Simoleon | |
4 // | |
5 // Created by Dennis Concepción Martín on 11/07/2021. | |
6 // | |
7 | |
8 import Foundation | |
9 | |
10 | |
11 /* | |
12 Read JSON File | |
13 */ | |
14 func parseJson<T: Decodable>(_ filename: String) -> T { | |
15 let data: Data | |
16 | |
17 guard let file = Bundle.main.url(forResource: filename, withExtension: nil) | |
18 else { | |
19 fatalError("Couldn't find \(filename) in main bundle.") | |
20 } | |
21 | |
22 do { | |
23 data = try Data(contentsOf: file) | |
24 } catch { | |
25 fatalError("Couldn't load \(filename) from main bundle:\n\(error)") | |
26 } | |
27 | |
28 do { | |
29 let decoder = JSONDecoder() | |
30 return try decoder.decode(T.self, from: data) | |
31 } catch { | |
32 fatalError("Couldn't parse \(filename) as \(T.self):\n\(error)") | |
33 } | |
34 } |