diff 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
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Simoleon/Helpers/FileHelper.swift	Sat Aug 28 11:15:25 2021 +0100
@@ -0,0 +1,41 @@
+//
+//  File.swift
+//  Simoleon
+//
+//  Created by Dennis Concepción Martín on 26/8/21.
+//
+
+import Foundation
+
+/*
+ Decode and read json file
+ */
+func read<T: Decodable>(json filename: String) throws -> T {
+    let data: Data
+    
+    guard let file = Bundle.main.url(forResource: filename, withExtension: nil)
+    else {
+        throw ErrorHandling.Json.fileMissing
+    }
+    
+    do {
+        data = try Data(contentsOf: file)
+    } catch {
+        throw ErrorHandling.Json.loadFailed(cause: error.localizedDescription)
+    }
+    
+    do {
+        let decoder = JSONDecoder()
+        return try decoder.decode(T.self, from: data)
+    } catch {
+        throw ErrorHandling.Json.parseFailed(cause: error.localizedDescription)
+    }
+}
+
+/*
+ Read configuration variables from Config.xconfig
+ */
+func readConfigVariable(withKey: String) -> String? {
+    return (Bundle.main.infoDictionary?[withKey] as? String)?
+        .replacingOccurrences(of: "\\", with: "")
+}