How would I go about extending an NSObject, NSStoryboardSegue.Identifier
, to provide a set of cases as potential parameter types?
extension NSStoryboardSegue.Identifier {
enum identifier: String {
case showHelpWindow
}
}
extension ViewController {
override func prepare(for segue: NSStoryboardSegue, sender: Any?) {
if (segue.identifier == "showHelpWindow") {
// What I have now
}
if segue.identifier == .showHelpWindow {
// What I'm trying to get
}
}
func showHelpWindow() {
// What I have now
performSegue(withIdentifier: "showHelpWindow", sender: self)
// What I'm trying to get
performSegue(withIdentifier: .showHelpWindow, sender: self)
}
}
2
Answers
Define a protocol
and a protocol extension to overload
performSegue
. ThesegueIdentifer(for
method is to get the identifier if there are multiple segues in the controllerTo use it adopt the protocol in the view controller and declare an enum for the identifiers
You don’t actually need to extend
NSStoryboardSegue.Identifier
at all. Just create your own enum:Add a custom
==
operator so that it can be compared withsegue.identifier
:Then extend
NSViewController
to add an overload ofperformSegue
that takesSegueIdentifier
:Usage: