select one item in an outlet collection - swift

211 Views Asked by At

How would I code: select the button who's titlelabel.text is equal to "category"

@IBOutlet var buttons: [UIButton]!

var category: String?

  override func viewDidLoad() {
    super.viewdidload()

      //I want to put the code here!

    }
1

There are 1 best solutions below

0
Jawad Ali On BEST ANSWER

Here is how you can filter out that button

let requiredButton = buttons
        .filter{ $0.titleLabel?.text == "category" }.first

Also you can achieve that with first

let butt =   buttons.first(where: { $0.titleLabel?.text == "category" })