TokiTrainer/Toki Trainer/Models/TokiJSONLoader.swift

56 lines
1.5 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
}
}
2021-11-08 17:24:46 +00:00
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
}
}
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
}
}