Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save theoknock/9e67b6d7d8d6f67ee3e7fb746f4f2b4b to your computer and use it in GitHub Desktop.
Save theoknock/9e67b6d7d8d6f67ee3e7fb746f4f2b4b to your computer and use it in GitHub Desktop.
Rewrite description
.fileImporter(isPresented: $isImporting, allowedContentTypes: [.plainText], allowsMultipleSelection: false) { result in
switch result {
case .success(let urls):
guard let url = urls.first else { return }
do {
// Ensure you have access to the resource
guard url.startAccessingSecurityScopedResource() else {
print("Access to the file was denied.")
return
}
defer {
// Stop accessing the resource after you're done
url.stopAccessingSecurityScopedResource()
}
let data = try Data(contentsOf: url)
let csvString = String(decoding: data, as: UTF8.self)
structs = parseCSV(csvString)
} catch {
print("Failed to read or decode file: \(error)")
}
case .failure(let error):
print("Failed to import: \(error)")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment