NSOpenPanel inheritance and "designated initializer"

Viewed 190

I am quite new to swift, coming from C++ without background in Obj-C, and I can't find and elegant solution to a small problem...

I use a NSOpenPanel to retrieve some files urls.

My code look likes that :

let import_panel = NSOpenPanel()
import_panel.allowsMultipleSelection = true;
import_panel.canChooseDirectories = false;
import_panel.canCreateDirectories = false;
import_panel.canChooseFiles = true;
let import_panel_delegate = ImportPanelDelegate.init()
import_panel.delegate = import_panel_delegate
import_panel.prompt = NSLocalizedString("IMPORT_BUTTON_TEXT", comment: "")
let i = import_panel.runModal();

I would like to use this import panel at several places in my app, so my first thought is to subclass NSOpenPanel to create an "ImportPanel".

class ImportPanel : NSOpenPanel{
  init()
  {
    super.init()
    self.allowsMultipleSelection = true
    self.canChooseDirectories = false
    self.canCreateDirectories = false
    self.canChooseFiles = true
    self.delegate = ImportPanelDelegate.init()
    self.prompt = NSLocalizedString("IMPORT_BUTTON_TEXT", comment: "")
  }
}

Here, I face a compile error, telling that I should use the "designated initializer" of NSOpenPanel. If I understand well, the designated initializer is an initializer that explicitly set multiple parameters. But here, I don't know which initializer use. Moreover, I don't know how to fill parameters for these different initializers !

I am aware that Swift is more "Protocol oriented programming" than "Object Oriented programming", so maybe subclassing NSOpenPanel was not the right solution, but I can't figure out how to use protocols here.

So, if I want to specialize a NSOpenPanel, how should I do it ?

Thanks by advance !

1 Answers
Related