comparison Simoleon/Helpers/FileHelper.swift @ 156:84137052813d

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