I have been using this block of Swift code for weeks with no problems. Then, today, every time I invoke it it fails:
let openPanel = NSOpenPanel()
openPanel.canChooseFiles = true
openPanel.allowsMultipleSelection = false
openPanel.canChooseDirectories = true
openPanel.canCreateDirectories = false
openPanel.title = NSLocalizedString("Open a CSV file", comment: "Open a CSV File")
var result = NSApplication.ModalResponse.OK
do {
try result = openPanel.runModal()
}
catch {
print("Open Panel failed: \(error)")
return
}
if result == .OK {
CSVFile = openPanel.url!.path
}
else {
openPanel.close()
print("No CSV file selected.")
return
}
I get this message pointing to the .openModal expression:
"Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value"
It's a real head-scratcher!