I have an array of horses that I am appending with the following code with a UIButton:
This is the array I am adding to:
I can add a random horse to the array, but if I click it again it adds the same horse again. I want it to be so that every time I click it, a new, different horse appears in the array. If I restart the app, though, I get a new horse when I click the button the first time (but the same if I click it again)
Code Block myHorses.append(Horse( name: "New Horse", gender: ["Mare", "Stallion"][Int.random(in:0...1)], age: Int.random(in:1...19) )
This is the array I am adding to:
Code Block var myHorses = [ Horse(name: "Donnerhall", gender: "Stallion", age: 5 ]
I can add a random horse to the array, but if I click it again it adds the same horse again. I want it to be so that every time I click it, a new, different horse appears in the array. If I restart the app, though, I get a new horse when I click the button the first time (but the same if I click it again)
I think I solved the issue. I added an ID number to each new horse, making all the names unique. Now each new horse is random and also has an ID number.
I made a property :
then each new horse got the property
and the buyHorseButton is assigned to do this after creating a horse;
I know this maybe isn't the cleanest fix, but I am really happy it's working. Couldn't have done it without your input! Thanks for your patience :)
I made a property :
Code Block var horsesCreated = 2
then each new horse got the property
Code Block idNumber: (horsesCreated+1)
and the buyHorseButton is assigned to do this after creating a horse;
Code Block horsesCreated += 1
I know this maybe isn't the cleanest fix, but I am really happy it's working. Couldn't have done it without your input! Thanks for your patience :)