4

In my UIViewController I've got outlet collection:

@IBOutlet var cardButtons: Array<UIButton>

Form this collection I want to get a index of object, but Array don't have a proper method for this. I found that 'indexOfObject' method is only in NSArray.

For now a I cast cardButtons property to NSArray

var cardButtonsArray = cardButtons as NSArray

and this solve my problem, but maybe there is another, more clean way to do this?

Thank you for your help.

LakaLe_
  • 454
  • 1
  • 6
  • 15
  • I googled 'indexOfObject in swift' for you. The top link is http://stackoverflow.com/questions/24028860/how-to-find-index-of-list-item-in-apples-swift – Mike Pollard Aug 22 '14 at 09:05

1 Answers1

0

firstIndex is a method you can use on a Swift Array that returns the first element's index that matches the object you pass in: https://developer.apple.com/documentation/swift/array/2994720-firstindex

func getIndexOf(button: UIButton) -> Int? {
    let buttonIndex = cardButtons.firstIndex(of: button)
    return buttonIndex
}