使用未解析的标识符 'isMatched'
Use of unresolved identifier 'isMatched'
我正在努力完成 Stanford Spring 2020 iOS course。
我的代码中出现了这个错误: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[[=11=]].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 = true
和 cards[potentialMatchIndex].isMatched = true
可能是问题所在。
数组[index] returns 可选。这是因为索引可能无效。
您需要解包该值才能访问 isMatched。
像这样:
if let chosenCard = cards[chosenIndex] as? Card {
chosenCard.isMatched = true
}
我正在努力完成 Stanford Spring 2020 iOS course。
我的代码中出现了这个错误: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[[=11=]].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 = true
和 cards[potentialMatchIndex].isMatched = true
可能是问题所在。
数组[index] returns 可选。这是因为索引可能无效。
您需要解包该值才能访问 isMatched。
像这样:
if let chosenCard = cards[chosenIndex] as? Card {
chosenCard.isMatched = true
}