Using an item's array index to look up data in the array?

Currently, I use the horse name to lookup data in the myHorses array. However, the app won't if the user uses the same name for multiple horses. Is it possible to use the array index instead? I want the app to create a stack of buttons equal to the number of horses created, then set the title for button one as the name of the horse at index [0], then the next button with the name of the horse at index [1] etc for all horses. Then when the user presses a button, the app uses the index instead of the name to lookup data for that horse. Not sure how to do this in practice

Thanks in advance for any tips :)

model
Code Block
var myHorses = [
    Horse(name: "Donnerhall", idNumber: 1",
Horse(name: "Mischa", idNumber: 2"
]
func countHorses() -> Int {
    myHorses.count
}
var horseNumber = countHorses()
    var currentHorse = myHorses[0]



view controller 1
Code Block
import UIKit
class HerdViewController: UIViewController {
    //Buttons stack
        for horse in myHorses {
            let button = UIButton()
            button.addTarget(self, action: #selector(self.lookupHorse(sender:)), for: .touchUpInside)
            button.setTitle(horse.name, for: UIControl.State.normal)
            buttonsStack.addArrangedSubview(button)
         }
         buttonsStack.translatesAutoresizingMaskIntoConstraints = false
    }
//change currentHorse var and segue to horse viewer
    @objc func lookupHorse(sender: UIButton){
        currentHorse = myHorses[1] //I put in the index for horse 2, but I want the app to figure this out based on the button pressed
        let horseName = sender.title(for: .normal)
            if let theHorse = myHorses.first(where: {$0.name == horseName})
                }
performSegue(withIdentifier: "horseViewerSegue", sender: self)
    }
}


VC 2
Code Block import UIKit
class HorseViewController: UIViewController {
override func viewDidLoad() {
        super.viewDidLoad()
}
override func viewWillAppear(_ animated: Bool) {
        horseNameOutlet.text = currentHorse.name
horseIdOutlet.text = currentHorse.idNumber
}



Answered by OOPer in 641284022
You can use tag of the UIButtons, if it is not used for other purpose.
Code Block
//...
for horseIndex in myHorses.indices { //<-
let horse = myHorses[horseIndex] //<-
let button = UIButton()
button.tag = horseIndex //<-
button.addTarget(self, action: #selector(self.lookupHorse(sender:)), for: .touchUpInside)
button.setTitle(horse.name, for: UIControl.State.normal)
buttonsStack.addArrangedSubview(button)
}
//...
@objc func lookupHorse(sender: UIButton) {
let index = sender.tag
currentHorse = myHorses[index]
//...
performSegue(withIdentifier: "horseViewerSegue", sender: self)
}


Accepted Answer
You can use tag of the UIButtons, if it is not used for other purpose.
Code Block
//...
for horseIndex in myHorses.indices { //<-
let horse = myHorses[horseIndex] //<-
let button = UIButton()
button.tag = horseIndex //<-
button.addTarget(self, action: #selector(self.lookupHorse(sender:)), for: .touchUpInside)
button.setTitle(horse.name, for: UIControl.State.normal)
buttonsStack.addArrangedSubview(button)
}
//...
@objc func lookupHorse(sender: UIButton) {
let index = sender.tag
currentHorse = myHorses[index]
//...
performSegue(withIdentifier: "horseViewerSegue", sender: self)
}


That worked! Thanks very much :D
Using an item's array index to look up data in the array?
 
 
Q