Iterating over a Dictionary where Key is String and Value is Selector using ...
for (b,a) in buttons {
let index = buttons.index(forKey: b)
}I want to get an Int from the Index ? from Dictionary<String,Selector>.Index?
You have no need to use `enumerate()` or get an Int from the Index, your `forEach` code can be re-written in something like this:
buttons.forEach {title, action in
let button = alert.addButton(withTitle: title)
button.action = action
}But, when you set up NSAlert with buttons, you may want to add buttons in a specific order.
You may need to remember that Dicitonary's order is not predictable.
Try executing this code repeatedly in the Playground:
let dict = ["Browse":"action01","Quit":"action02"]
dict.forEach {title, action in
print(title, action)
}Sometimes it outputs...
Browse action01
Quit action02
But sometimes...
Quit action02
Browse action01
You should better change the header of the method to use Array of tuples, not Dictionary.
static func showMessage(_ message: String,_ infoline: String, buttons: [(String,Selector)]) {And about your main concern getting "unrecognized selector sent to instance".
Have you set the target of the button? You may need it to include in the tuple:
static func showMessage(_ message: String,_ infoline: String, buttons: [(String,AnyObject,Selector)]) { //<-
let alert = NSAlert()
//...
buttons.forEach {title, target, action in
let button = alert.addButton(withTitle: title)
button.action = action
button.target = target //<-
}
//...
}