Created
January 28, 2024 06:27
-
-
Save theoknock/9e67b6d7d8d6f67ee3e7fb746f4f2b4b to your computer and use it in GitHub Desktop.
Rewrite description
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.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