I'm new to Swift and I'd like to know how I can take some values from an array and turn them into buttons in a stack. I have an array with dogs and their info. I want the buttons to display the name of each dog. The number of dogs can change, so I need the stack to change to reflect what is in the array. I am really lost, I know I need to be adding this code to the view controller but I can't find anything similar to this
This is my array:
var myDogs = [
Dog(name: "Saleks", gender: "Male", speed: 50),
Dog(name: "Balto", gender: "Male", speed: 70),
Dog(name: "Mila", gender: "Female", speed: 20)
]
This is my viewController:
@IBOutlet weak var buttonsStack: UIStackView!
override func viewDidLoad() {
super.viewDidLoad()
for Dog in myDogs {
print("\(Dog.name)")
}
//adding a new dog to the array
@IBAction func buyDogButton(_ sender: UIButton) {
func buyDog() {
myDogs.append(Dog(name: "Dogname", gender: "female", speed: 20))
}