comparison Simoleon/Helpers/FileHelper.swift @ 171:70f0625bfcf1

Merge pull request #17 from denniscm190/development open source project committer: GitHub <noreply@github.com>
author Dennis C. M. <dennis@denniscm.com>
date Tue, 12 Oct 2021 16:17:35 +0200
parents 0c589138a6f3
children
comparison
equal deleted inserted replaced
146:f10b0e188905 171:70f0625bfcf1
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 func readJson<T: Decodable>(from filename: String) throws -> T {
12 let data: Data
13
14 guard let file = Bundle.main.url(forResource: filename, withExtension: nil)
15 else {
16 throw ErrorHandling.Json.fileMissing
17 }
18
19 do {
20 data = try Data(contentsOf: file)
21 } catch {
22 throw ErrorHandling.Json.loadFailed(cause: error.localizedDescription)
23 }
24
25 do {
26 let decoder = JSONDecoder()
27 return try decoder.decode(T.self, from: data)
28 } catch {
29 throw ErrorHandling.Json.parseFailed(cause: error.localizedDescription)
30 }
31 }
32
33 func readConfig(withKey: String) -> String? {
34 return (Bundle.main.infoDictionary?[withKey] as? String)?
35 .replacingOccurrences(of: "\\", with: "")
36 }