Swift: Generate Identifier for Variable

154 views Asked by At

I have a large list of NSButtons (80+) that I assign to an array. I've used a numbering scheme to name them and I'd like to generate the identifiers for them instead of calling them directly by name one at a time.

@IBOutlet weak var button120: NSButton!
@IBOutlet weak var button121: NSButton!
var buttons [NSButton]()

Currently (in ViewDidLoad()) I'm doing the equivalent of:

buttons = [ button120, button121 ]  

I'd prefer to do something like:

for index in 120...121 {
  buttons.append("button\(index)".toIdentifier)
}

Can this be done in Swift?

1

There are 1 answers

4
vadian On BEST ANSWER

This might be an easier solution, view is the parent view of the buttons

let buttons = view.subviews.filter { $0 is NSButton}