diff Simoleon/Helpers/ReadJson.swift @ 183:d2398f02e1ce

implement unit currency selector
author Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
date Mon, 20 Dec 2021 12:28:16 +0100
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Simoleon/Helpers/ReadJson.swift	Mon Dec 20 12:28:16 2021 +0100
@@ -0,0 +1,30 @@
+//
+//  ReadJson.swift
+//  Simoleon
+//
+//  Created by Dennis Concepción Martín on 8/12/21.
+//
+
+import Foundation
+
+func readJson<T: Decodable>(from filename: String) -> T {
+    let data: Data
+    
+    guard let file = Bundle.main.url(forResource: filename, withExtension: nil)
+    else {
+        fatalError("Failed to locate \(filename)")
+    }
+    
+    do {
+        data = try Data(contentsOf: file)
+    } catch {
+        fatalError("Failed to load \(filename)")
+    }
+    
+    do {
+        let decoder = JSONDecoder()
+        return try decoder.decode(T.self, from: data)
+    } catch {
+        fatalError("Failed to decode \(filename)")
+    }
+}