Checking object property in an array

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

Does the code run successfully?

Hi Chris, yes runs fine and I’ve verified it numerous times.

In that case it’s a good result and is an alternative way to achieve the same outcome. Well done.

I’ll add that method to my version of the project too just for the heck of it.

sounds good! If I used a loop I would know exactly what the code is doing, I wanted to see if there was a way to check an array full of objects if a particular object had a matching property. I expect I will need this type of capability in the future. I can see from testing this works, but I’m not sure exactly what the code is doing, I assume where: { $0.imageName == cardOne.imageName}) is instructing the code to check all objects in the array with the property imageName so essentially this is a loop but would be good to know exactly what is going on

You can always check the code in the Swift standard library to see what’s going on.

In the case of contains(where:), it’s in the SequenceAlgorithms.swift file and there we can see that it does indeed use a loop to check every item until it finds one that passes the given predicate:

@inlinable
  public func contains(
    where predicate: (Element) throws -> Bool
  ) rethrows -> Bool {
    for e in self {
      if try predicate(e) {
        return true
      }
    }
    return false
  }
1 Like

thats great thanks! didn’t know that :wink: