TokiTrainer/Toki Trainer/Models/TokiJSONLoader.swift
2021-11-08 12:24:46 -05:00

56 lines
1.5 KiB
Swift

//
// TokiJSONLoader.swift
// TokiJSONLoader
//
// Created by Avery Ada Pace on 10/1/21.
//
import Foundation
class TokiJSONLoader: ObservableObject {
func loadDictionary() -> [TokiDictEntry]? {
let jsonData = loadJSON("toki-dictionary")
do {
let decodedData = try JSONDecoder().decode([TokiDictEntry].self, from: jsonData!)
return decodedData
} catch {
print("Decode error: \(error)")
return nil
}
}
func loadPOS() -> [TokiPartOfSpeech]? {
let jsonData = loadJSON("toki-partsofspeech")
do {
let decodedData = try JSONDecoder().decode([TokiPartOfSpeech].self, from: jsonData!)
return decodedData
} catch {
print("Decode error: \(error)")
return nil
}
}
func loadLessons() -> [TokiLesson]? {
let jsonData = loadJSON("toki-lessons")
do {
let decodedData = try JSONDecoder().decode([TokiLesson].self, from: jsonData!)
return decodedData
} catch {
print("Decode error: \(error)")
return nil
}
}
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
}
}