runModal exits with a fatal error.

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!

Thanks for the post.

What about using a if let here instead CSVFile = openPanel.url!.path

if let url = openPanel.url {
CSVFile = url.path
}

If the url is nil, will definitely crash! (no exclamation point pun intended)

Hope this helps

Albert Pascual
  Worldwide Developer Relations.

runModal exits with a fatal error.
 
 
Q