I am trying to pick and run a random function in swift.
Currently I have implemented a switch case like this
switch Int.random(in: 1...2) {
case 1: functionOne()
case 2: functionTwo()
default: print("error")
}
if there are more options the code gets really excessive.
How can I improve the code to make it save the extra lines? The optimum would be:
[functionOne(), functionTwo()].randomElement()
This does not work but just runs the last element in the array. Is there a way to put functions in an array and let them run?
Aucun commentaire:
Enregistrer un commentaire