I have managed get my code to randomly pick objects and display the Images on my view controller but sometimes the randomly selected object will be a duplicate so I need to be able to remove the selected ones that get stored in the second array from the first array so they can not be chosen for the second time but I am unsure how to get this to happen in my code, Here is my code that works but will potentially select duplicates:
@IBAction func drawCardsButtonPressed(_ sender: Any) {
cardsDrawnArray = []
if cardsDrawn == 1 {
let randomCards = cardsArray[Int(arc4random_uniform(UInt32(cardsArray.count)))]
cardsDrawnArray.append(randomCards.tarotImage)
tarotCardFive.image = cardsDrawnArray[0]
} else if cardsDrawn == 3 {
for _ in 0...2 {
let randomCards = cardsArray[Int(arc4random_uniform(UInt32(cardsArray.count)))]
cardsDrawnArray.append(randomCards.tarotImage)
}
tarotCardFour.image = cardsDrawnArray[0]
tarotCardFive.image = cardsDrawnArray[1]
tarotCardSix.image = cardsDrawnArray[2]
}
else if cardsDrawn == 5 {
for _ in 0...4 {
let randomCards = cardsArray[Int(arc4random_uniform(UInt32(cardsArray.count)))]
cardsDrawnArray.append(randomCards.tarotImage)
}
tarotCardTwo.image = cardsDrawnArray[0]
tarotCardFour.image = cardsDrawnArray[1]
tarotCardFive.image = cardsDrawnArray[2]
tarotCardSix.image = cardsDrawnArray[3]
tarotCardEight.image = cardsDrawnArray[4]
} else if cardsDrawn == 9 {
for _ in 0...8 {
let randomCards = cardsArray[Int(arc4random_uniform(UInt32(cardsArray.count)))]
cardsDrawnArray.append(randomCards.tarotImage)
}
tarotCardOne.image = cardsDrawnArray[0]
tarotCardTwo.image = cardsDrawnArray[1]
tarotCardThree.image = cardsDrawnArray[2]
tarotCardFour.image = cardsDrawnArray[3]
tarotCardFive.image = cardsDrawnArray[4]
tarotCardSix.image = cardsDrawnArray[5]
tarotCardSeven.image = cardsDrawnArray[6]
tarotCardEight.image = cardsDrawnArray[7]
tarotCardNine.image = cardsDrawnArray[8]
} else {
print(" Invalid Number")
}
}
Any help to fix this is much appreciated.