matchCardGame winning condition

Hello Chris, great video. You explain everything so throughly that it is easy to follow. I accidentally casted the function checkForMatch(indexPath) in the if-code instead of doing it in the else-code. What happens is that the card instantly disappears. Why is that? I checked it with print(…) so see what it does put I couldn’t figure out why it gives the card the same imageNames which is the condition for checkForMatch()? Pls Help me understand. Cheers Michael

Hi Michael,

If you check for a match straight after the first card has been flipped then in the checkForMatches function cardOneCell and cardTwoCell are set to the same card so the following tests in that function evaluate to the same card. ie, a match.

To check this for yourself, set a breakpoint in checkForMatches at the first line of code where it says:

let cardOneCell = collectionView.cellForItem(at: firstFlippedCardIndex!) as ? CardCollectionViewCell

Step forward to the third line of code and then in the console type the command:

po cardOneCell

That means “Print out the contents of cardOneCell to the console”
Do the same for cardTwoCell:

po cardTwoCell

The following is a sample of the output. You can see that both cardOneCell and cardTwoCell point to the same card.

With the code in the right place, cardOneCell and cardTwoCell are set to two different memory addresses corresponding to the two different cards. Sample image from the console output attached:

Great - Thanks for your help Chris.