Mercurial > public > simoleon
comparison Simoleon/Jobs/FileController.swift @ 154:8afba86ab8dd
Refactor code
author | Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com> |
---|---|
date | Wed, 25 Aug 2021 10:43:12 +0100 |
parents | |
children | 681f2cbe8c7f |
comparison
equal
deleted
inserted
replaced
153:2590ee472aa9 | 154:8afba86ab8dd |
---|---|
1 // | |
2 // ReadConfig.swift | |
3 // Simoleon | |
4 // | |
5 // Created by Dennis Concepción Martín on 20/07/2021. | |
6 // | |
7 | |
8 import Foundation | |
9 | |
10 class FileController { | |
11 | |
12 /* | |
13 Read configuration variables from Config.xconfig | |
14 */ | |
15 func readConfigVariable(withKey: String) -> String? { | |
16 return (Bundle.main.infoDictionary?[withKey] as? String)? | |
17 .replacingOccurrences(of: "\\", with: "") | |
18 } | |
19 | |
20 /* | |
21 Decode and read json file | |
22 */ | |
23 func read<T: Decodable>(json filename: String) throws -> T { | |
24 let data: Data | |
25 | |
26 guard let file = Bundle.main.url(forResource: filename, withExtension: nil) | |
27 else { | |
28 throw JsonErrors.fileMissing | |
29 } | |
30 | |
31 do { | |
32 data = try Data(contentsOf: file) | |
33 } catch { | |
34 throw JsonErrors.loadFailed(cause: error.localizedDescription) | |
35 } | |
36 | |
37 do { | |
38 let decoder = JSONDecoder() | |
39 return try decoder.decode(T.self, from: data) | |
40 } catch { | |
41 throw JsonErrors.parseFailed(cause: error.localizedDescription) | |
42 } | |
43 } | |
44 } |