Rough dictionary list working
This commit is contained in:
19
Toki Trainer/Models/TokiDictionary.swift
Normal file
19
Toki Trainer/Models/TokiDictionary.swift
Normal 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
|
||||
}
|
||||
|
46
Toki Trainer/Models/TokiJSONLoader.swift
Normal file
46
Toki Trainer/Models/TokiJSONLoader.swift
Normal 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
|
||||
}
|
||||
}
|
13
Toki Trainer/Models/TokiPartOfSpeech.swift
Normal file
13
Toki Trainer/Models/TokiPartOfSpeech.swift
Normal 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
|
||||
}
|
Reference in New Issue
Block a user