I’m engaged on importing flashcards from a CSV file in my Swift app. I exploit the next methodology 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 {
attempt CSVString.write(to: fileURL, atomically: true, encoding: .utf8)
DispatchQueue.principal.async {
self.csvFileURL = fileURL
}
self.isShowingExportView = true
} catch {
print("Error writing CSV: (error.localizedDescription)")
}
}
Then I try and import it utilizing:
func importFlashcards(from fileURL: URL) {
do {
let content material = attempt String(contentsOf: fileURL, encoding: .utf8)
let rows = content material.parts(separatedBy: "n").dropFirst() // Skip header
for row in rows {
let trimmed = row.trimmingCharacters(in: .whitespacesAndNewlines)
guard !trimmed.isEmpty else { proceed }
var parts = trimmed.parts(separatedBy: "", "")
if parts.depend == 2 {
// Take away surrounding quotes
var reply = parts[0]
var query = parts[1]
if reply.hasPrefix(""") { reply.removeFirst() }
if query.hasSuffix(""") { query.removeLast() }
dataController.newFlashcard(reply: reply, query: query)
}
}
} catch {
print("Didn't load flashcards: (error.localizedDescription)")
}
}
Nonetheless, after I choose the file for import, I get the next error:
Didn't 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 positive.