If your data are static, or change only infrequently, putting them into the app is fine. If the data change, and you are distributing the app to others, you will need to replace the app’s data file, recompile, and re-release the app. There’s a way of using files from the Documents folder of iOS, or “external” files, but this can be a bit of a learning curve.
Assuming that your data are static, then here’s what to do:
Place your term/phrase csv file into your App’s project folder using MacOS’ Finder, preferably using Copy (not Move), so as to keep a backup copy.
In Xcode, select File -> Add Files to…. From the Top Menu and a pop-up screen will appear
Select your file from the list (others should be greyed out) and make sure that the Destination - copy files if needed box is ticked and the Add to targets box - YourAppName is also ticked
Press the Add button (far right, bottom of pop-up)
Back in Xcode’s main screen, replace the getData function in DataModel with:
func getData() {
// DatFrame(contentsOfCSVFile) needs a URL reference, so we need to get the app's data (Bundle resource) reference as a URL
guard let fileURL = Bundle.main.url(forResource: "TermPhrases", withExtension: "csv") else {
// CAUTION - withExtension is case sensitive, so if your CSV file was created as TermPhrases.CSV then you need to use uppercase in the parameter
print("**** error getting file url")
return
}
let csvOptions = CSVReadingOptions(hasHeaderRow: true, ignoresEmptyLines: true, delimiter: ",")
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
}
print("**** Loaded \(dataTable.rows.count) Terms") // check to make sure terms got loaded
}
I haven't tested this with iOS (just on the Mac), but it should work - assuming that you created a new iOS project or multi-platform project with Shared resources.
Best wishes and regards, Michaela