I've been following this Stanford course on swiftUI and I've gotten to a point where im 100% sure my code is essentially identical to what i'm following but it doesn't work, I've been trying to debug it but for some reason, that I don't understand the view doesn't update.
view-model:
class CardModel: ObservableObject
{
// view model
static var items = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T",]
static var count = 3
static func createMemoryGame() -> MemoryGame<String>
{
MemoryGame<String>(numberOfPairsOfCards: CardModel.count, createCardContent:
{
pairIndex in
CardModel.items[pairIndex]
})
}
@Published private var model: MemoryGame<String> = CardModel.createMemoryGame()
var cards: Array<MemoryGame<String>.Card>
{
return model.cards
}
// MARKS: - Intent(s)
func choose(_ card: MemoryGame<String>.Card)
{
print("debug func choose in cardmodel")
model.choose(card)
}
}
view:
struct ContentView: View{
@ObservedObject var viewModel: CardModel
var body: some View
{
VStack
{
LazyVGrid(columns: [GridItem(.adaptive(minimum: 75))])
{
ForEach(viewModel.cards)
{
card in
CardView(card: card).aspectRatio(2/3, contentMode: .fit)
.onTapGesture {
print("\(card.isFaceUp)")
viewModel.choose(card)
print("\(card.isFaceUp)")
}
}
}
Spacer()
.foregroundColor(.red)
}
.padding(.horizontal)
.font(.largeTitle)
}
}
model:
struct MemoryGame<CardContent>
{
private(set) var cards: Array<Card>
mutating func choose(_ card: Card)
{
let chosenIndex = index(of: card)
cards[chosenIndex].isFaceUp = !cards[chosenIndex].isFaceUp
print("debug func choose in memorygame")
}
func index(of card: Card) -> Int
{
for index in 0..<cards.count
{
if cards[index].id == card.id
{
print("debug func index")
return index
}
}
return 0
}
when I uncomment all the debug comments the output is
-true -debug func choose in cardmodel -debug func index -debug func choose in memorygame -true
https://www.youtube.com/watch?v=oWZOFSYS5GE heres the video I'm stuck on all my code passes all the same debug tests up until 32:48