TokiTrainer/Toki Trainer/Models/TokiJSONLoader.swift

45 lines
1.2 KiB
Swift
Raw Normal View History

2021-11-03 18:21:22 +00:00
//
// TokiJSONLoader.swift
// TokiJSONLoader
//
// Created by Avery Ada Pace on 10/1/21.
//
import Foundation
class TokiJSONLoader: ObservableObject {
func loadDictionary() -> [TokiDictEntry]? {
2021-11-03 18:21:22 +00:00
let jsonData = loadJSON("toki-dictionary")
do {
let decodedData = try JSONDecoder().decode([TokiDictEntry].self, from: jsonData!)
return decodedData
2021-11-03 18:21:22 +00:00
} catch {
print("Decode error: \(error)")
return nil
2021-11-03 18:21:22 +00:00
}
}
func loadPOS() -> [TokiPartOfSpeech]? {
2021-11-03 18:21:22 +00:00
let jsonData = loadJSON("toki-partsofspeech")
do {
let decodedData = try JSONDecoder().decode([TokiPartOfSpeech].self, from: jsonData!)
return decodedData
2021-11-03 18:21:22 +00:00
} catch {
print("Decode error: \(error)")
return nil
2021-11-03 18:21:22 +00:00
}
}
func loadJSON(_ resource: String) -> Data? {
do {
if let bundlePath = Bundle.main.path(forResource: resource, ofType: "json"), let jsonData = try String(contentsOfFile: bundlePath).data(using: .utf8) {
return jsonData
}
} catch {
print(error)
}
return nil
}
}