iosswiftcs193p

Use of unresolved identifier 'isMatched'


I am working my way through the Stanford Spring 2020 iOS course.

I got this error in my code: Use of unresolved identifier 'isMatched' (Actually Xcode displays this same error two times).

Please help me :( I can't see anything wrong in my code. Here's my code:

import Foundation
import SwiftUI

struct MemoryGame<CardContent> where CardContent: Equatable {
    var cards: Array<Card>
    var theme: Theme //Assignment 2
    var score: Int = 0
    
    var indexOfOneAndOnlyFaceUpCard: Int? {
        get { cards.indices.filter { cards[$0].isFaceUp }.only }
        set {
            for index in cards.indices { // sets all other than current "indexOfOneAndOnlyFaceUpCard" cards to isFaceUp = false
                cards[index].isFaceUp = index == newValue
            }
        }
    }
    
    mutating func choose(card: Card) {
        if let chosenIndex = cards.firstIndex(matching: card), !cards[chosenIndex].isFaceUp, !cards[chosenIndex].isMatched { //if chosen card is not faceUp and not yet matched
            if let potentialMatchIndex = indexOfOneAndOnlyFaceUpCard { // if one card is already face up
                if cards[chosenIndex].content == cards[potentialMatchIndex].content { // if already faceUp card = chosen card
                    cards[chosenIndex].isMatched = true
                    cards[potentialMatchIndex].isMatched = true
                    score += 2
                } else /* if both cards not the same */ {
                    //if card.isSeen {score -= 1} // and the currently choosen one isSeen - my bad solution
                }
                self.cards[chosenIndex].isFaceUp = true
            } else { // if this (currently choosen card) is only faceUp card
                indexOfOneAndOnlyFaceUpCard = chosenIndex
            }
        }
        // Assignment 2 - probably good fragment
        if card.isSeen == false {
            if let chosenIndex = cards.firstIndex(matching: card) {
                cards[chosenIndex].isSeen = true
            }
        }
        
    }
    
    init(numberOfPairsOfCards: Int, theme: Theme, cardContentFactory: (Int) -> CardContent) {// Assignment 2 theme: Theme
        cards = Array<Card>()
        for pairIndex in 0..<numberOfPairsOfCards {
            let content = cardContentFactory(pairIndex)
            cards.append(Card(content: content, id: pairIndex*2))
            cards.append(Card(content: content, id: pairIndex*2+1))
        }
        cards.shuffle()
        self.theme = theme
    }
    
    struct Card: Identifiable {
        var isFaceUp: Bool = false
        var isMatched: Bool = false
        var isSeen: Bool = false
        var content: CardContent
        var id: Int
    }
    
    // Assignment 2:
    struct Theme {
        let name: String
        let emojis: [CardContent]
        let numberOfCardPairsToShow: Int?
        let color: Color //import swiftui
        
        init(name: String, emojis: [CardContent], numberOfCardPairsToShow: Int? = nil, color: Color) {
            self.name = name
            self.emojis = emojis
            self.numberOfCardPairsToShow = numberOfCardPairsToShow
            self.color = color
        }
        
    }
}

Solution

  • The places where you have cards[chosenIndex].isMatched = true and cards[potentialMatchIndex].isMatched = true could be the issue.

    The array[index] returns an optional. This is because the index could be invalid.

    You need to unwrap the value before you can access isMatched.

    Like so :

    if let chosenCard = cards[chosenIndex] as? Card {
         chosenCard.isMatched = true
    }