Rough dictionary list working

This commit is contained in:
Avery Pace
2021-11-03 14:21:22 -04:00
parent 4f342ce48d
commit 731a71904a
10 changed files with 1980 additions and 90 deletions

View File

@@ -0,0 +1,19 @@
//
// TokiDictionary.swift
// TokiDictionary
//
// Created by Avery Ada Pace on 10/1/21.
//
import Foundation
struct TokiDictEntry: Decodable {
var word: String
var definitions: [TokiDictDefinition]
}
struct TokiDictDefinition: Decodable {
var pos: String
var definition: String
}

View File

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

View File

@@ -0,0 +1,13 @@
//
// TokiPartsOfSpeech.swift
// Toki Trainer
//
// Created by Avery Ada Pace on 11/2/21.
//
import Foundation
struct TokiPartOfSpeech: Decodable {
var pos: String
var definition: String
}