I’m engaged on importing flashcards from a CSV file in my Swift app. I take advantage of the next technique to generate and write the CSV file:
func generateCSVText(withManagedObjects arrManagedObject: [Flashcard]) {
var CSVString = "reply, questionn"
for flashcard in arrManagedObject {
let entityContent = ""(flashcard.flashcardAnswer)", "(flashcard.flashcardQuestion)"n"
CSVString.append(entityContent)
}
let tempDirectory = FileManager.default.temporaryDirectory
let fileURL = tempDirectory.appendingPathComponent("(cardSet.cardSetName).csv")
do {
strive CSVString.write(to: fileURL, atomically: true, encoding: .utf8)
DispatchQueue.major.async {
self.csvFileURL = fileURL
}
self.isShowingExportView = true
} catch {
print("Error writing CSV: (error.localizedDescription)")
}
}
Then I try to import it utilizing:
func importFlashcards(from fileURL: URL) {
do {
let content material = strive String(contentsOf: fileURL, encoding: .utf8)
let rows = content material.elements(separatedBy: "n").dropFirst() // Skip header
for row in rows {
let trimmed = row.trimmingCharacters(in: .whitespacesAndNewlines)
guard !trimmed.isEmpty else { proceed }
var elements = trimmed.elements(separatedBy: "", "")
if elements.rely == 2 {
// Take away surrounding quotes
var reply = elements[0]
var query = elements[1]
if reply.hasPrefix(""") { reply.removeFirst() }
if query.hasSuffix(""") { query.removeLast() }
dataController.newFlashcard(reply: reply, query: query)
}
}
} catch {
print("Did not load flashcards: (error.localizedDescription)")
}
}
Nevertheless, after I choose the file for import, I get the next error:
Did not load flashcards: ERR257.DFU
I additionally opened the CSV in a web based CSV editor and viewer, chosen “UTF-8” encoding there, and it displayed/edited simply tremendous.