繁体   English   中英

使用未解析的标识符“isMatched”

[英]Use of unresolved identifier 'isMatched'

我正在努力完成斯坦福大学 2020 年春季 iOS 课程

我的代码中出现此错误: Use of unresolved identifier 'isMatched' (实际上 Xcode 两次显示相同的错误)。

请帮帮我 :( 我看不出我的代码有什么问题。这是我的代码:

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
        }
        
    }
}

您拥有cards[chosenIndex].isMatched = truecards[potentialMatchIndex].isMatched = true可能是问题所在。

array[index] 返回一个可选值。 这是因为索引可能无效。

您需要先解开该值,然后才能访问 isMatched。

像这样:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM