我正在研究斯坦福在线 CS139p 中的示例。记忆游戏的卡片被绘制出来。我将 viewModel 设为 ObservableObject,发布代表记忆游戏的 var,然后运行应用程序。一切都按我的预期发生。然后,我将 @ObservedObject 属性处理器添加到 View Controller 中的 viewModel var。当我运行应用程序时,我不再看到卡片被抽出。
问题1:发生了什么?
问题2:我将如何调试这个?我尝试设置断点并遍历,但我无法确定出了什么问题。我用谷歌搜索并找到了一些可以尝试的东西,但最终无法让它发挥作用。一年前,当我第一次尝试时,这个例子对我有用。
MemorizeApp.swift
import SwiftUI
@main
struct MemorizeApp: App {
var body: some Scene {
WindowGroup {
let game = EmojiMemoryGame()
EmojiMemoryGameView(viewModel: game)
}
}
}
EmojiMemoryGameView.swift
import SwiftUI
// uncomment the below line and then comment the line below it to see the condition that does not work.
struct EmojiMemoryGameView: View {
// @ObservedObject var viewModel: EmojiMemoryGame
var viewModel:EmojiMemoryGame
var body: some View {
HStack {
ForEach(viewModel.cards) { card in
CardView(card: card).onTapGesture {
viewModel.choose(card: card)
}
}
}
.foregroundColor(Color.orange)
.padding()
.font(viewModel.cards.count == 10 ? Font.title : Font.largeTitle)
}
}
struct CardView: View {
var card: MemoryGame<String>.Card
var body: some View {
ZStack {
if card.isFaceUp {
RoundedRectangle(cornerRadius: 10.0).fill(Color.white)
RoundedRectangle(cornerRadius: 10.0).stroke(lineWidth: 3.0)
Text(card.content)
} else {
RoundedRectangle(cornerRadius: 10.0).fill()
}
}
.aspectRatio(2/3, contentMode: .fit)
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
EmojiMemoryGameView(viewModel: EmojiMemoryGame())
}
}
EmojiMemoryGame.swift
import SwiftUI
func createCardContent(pairIndex: Int) -> String {
return ""
}
// This is the viewModel
class EmojiMemoryGame: ObservableObject {
@Published private var model: MemoryGame<String> = EmojiMemoryGame.createMemoryGame()
static func createMemoryGame() -> MemoryGame<String> {
let emojies = ["", "", "", "", "", "", "", "", "", "", "", ""]
return MemoryGame<String>(numberOfPairsOfCards: Int.random(in: 2...5)) { pairIndex in
return emojies.randomElement()!
//return emojies[pairIndex]
}
}
// MARK: Access to the Model
var cards: Array<MemoryGame<String>.Card> {
model.cards.shuffle()
return model.cards
}
// MARK: Intent(s)
func choose(card: MemoryGame<String>.Card) {
model.choose(card: card)
}
}
记忆游戏.swift
import Foundation
struct MemoryGame<CardContent> {
var cards: Array<Card>
mutating func choose(card: Card) {
print("Card Choosen: \(card)")
let chosenIndex: Int = index(of: card)
cards[chosenIndex].isFaceUp = !cards[chosenIndex].isFaceUp
}
func index(of card: Card) -> Int {
for index in 0..<cards.count {
if self.cards[index].id == card.id {
return index
}
}
return 0 // TODO: bogus!
}
init(numberOfPairsOfCards: Int, cardContentFactory: (Int) -> CardContent) {
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))
}
}
struct Card: Identifiable {
var isFaceUp: Bool = true
var isMatched: Bool = false
var content: CardContent
var id: Int
}
}