I have this Objective-C Code :
- (IBAction)selectFileButtonAction:(id)sender {
//create open panel...
NSOpenPanel* openPanel = [NSOpenPanel openPanel];
my two cents for swift 5.0
override func showChooseFileDialog(title: String){
let openPanel = NSOpenPanel()
openPanel.canChooseFiles = false
openPanel.allowsMultipleSelection = false
openPanel.canChooseDirectories = false
openPanel.canCreateDirectories = false
openPanel.title = title
openPanel.beginSheetModal(for:self.view.window!) { (response) in
if response == .OK {
let selectedPath = openPanel.url!.path
// do whatever you what with the file path
}
openPanel.close()
}
}