Hi folks, do you see any issues referencing an object property with this line of code below?
if generatedCards.contains(where: { $0.imageName == cardOne.imageName}) {
my assumption is this will check all objects in the array for a match against the imageName
thoughts?
thanks
Kieran
func getCards() -> [Card] {
// Declare an empty array
var generatedCards = [Card]()
// Randomly generate 8 pairs of cards
for _ in 1...8 {
// Ensure no duplicate card pairs
var duplicateCard : Bool
// Create two new card objects
let cardOne = Card()
let cardTwo = Card()
repeat {
// Generate a random number
let randomNumber = Int.random(in: 1...13)
//Set their image names
cardOne.imageName = "card\(randomNumber)"
cardTwo.imageName = "card\(randomNumber)"
//Check if card already exists in array
if generatedCards.contains(where: { $0.imageName == cardOne.imageName}) {
duplicateCard = true
} else {
duplicateCard = false
}
} while duplicateCard == true
// Add them to the array
generatedCards += [cardOne, cardTwo]
//print("Card Added ----> \(cardOne.imageName)")
}
// Randomize the cards within the array
generatedCards.shuffle()
// Return the array
return generatedCards