The answers to your questions depend what you want to do with the "Dictionary", how volatile it is (i.e. updates, additions, deletions) and how large it is. A few hundred Terms that never change could be handled without a database (i.e. without an indexed, structured permanent store such as SQLite or CoreData). From my understanding of what you're looking for, I'd suggest creating your terms/phrases in a spreadsheet (e.g. Excel) and then exporting the table as a CSV file for use with code like in the SwiftUI sample below:
ContentView
import SwiftUI
import TabularData
struct ContentView: View {
var model = DataModel.shared
var body: some View {
List(model.dataTable.rows,id:\.index) { row in
HStack{
Text(row["Term"] as! String)
Text(String(row["Phrase"] as! String))
}
}
}
}
Data Model - this is not a database: it's where the importing and processing of the terms takes place within the app, and the data have to be loaded into the app again when next run.
import Foundation
import TabularData
class DataModel {
static let shared = DataModel()
@Published var dataTable = DataFrame()
init() {
getData()
let searchTerm = "Nine"
print("there are \(findTerm(searchTerm).rows.count) terms for \(searchTerm)")
}
func getData() {
var url: URL?
do {
url = try FileManager.default.url(for: FileManager.SearchPathDirectory.downloadsDirectory, in: FileManager.SearchPathDomainMask.userDomainMask, appropriateFor: nil, create: true)
} catch {
print("Failed to get Downsloads URL \(error)")
return
}
let csvOptions = CSVReadingOptions(hasHeaderRow: true, ignoresEmptyLines: true, delimiter: ",")
let fileURL = url?.appendingPathComponent("TermPhrases.csv")
do {
dataTable = try DataFrame(contentsOfCSVFile: fileURL!,columns: nil, rows: nil, types: ["Term":CSVType.string,"Phrase":CSVType.string], options: csvOptions)
} catch {
print("Failed to load datatable from CSV \(error)")
return
}
}
func findTerm(_ searchTerm: String) -> DataFrame.Slice {
let results = dataTable.filter ({ row in
guard let term = row["Term"] as? String else {
return false
}
if term == searchTerm {
return true
}
return false
})
return results
}
}
extension DataFrame.Rows : RandomAccessCollection {
}
Thiis sample uses the TabularData Framework which has many features for importing and manipulating data from CSV files (spreadsheet output), but also for Machine Learning. This sample app was written for MacOS using csv input from the Downloads folder:
Sample Data in a file called TermPhrases.csv
Term Phrase
Nine A stitch in time saves these
Two A bird in the hand is worth these in the bush
Seven Pillars of wisdom
The findTerm function in the DataModel shows how to filter (Search) the Data Table for a term. DataFrame filtering is not like normal Swift array filtering.
Hopefully this gets you started, then you can decide what other processing you'd like to do on the Data Table (i.e. your dictionary)
Best wishes and regards, Michaela