Identifiable
Struct
's mutating functionObserableObject
Enum
If we think of a View as just an agent for showing what in the Model through the ViewModel, then more likely, we're gonna pass it to it as an argument.
ObservableObject
讓 ViewModel conform protocol ObservableObject
⬅️ can publish somthing changed
SwiftUI will automatically monitor for such changes, and re-invoke the body property of any views that rely on the data.
(hackingwithswift) What is the @Published property wrapper?
@ObservedObject
means that when this says something changed, please rebuild my entire body.var
➡️ 完成「一旦 Model 更新,View 自動連動更新」
Now this enum will have a static var allCases
that you can iterate over.
An Optional
is just an enum.
func index(of card:)
換成 firstIndex(where: )
開始思考如何「讓場上至多兩張卡片是翻開」的遊戲規則
兩兩對應的卡片被翻開後 UI 沒有更新,卡片又被蓋回去了
→ view 那邊去檢查 isMatched 狀態
Model R&R
MemoryGame<CardContent>
),跟 UI 毫無關係(不需要 import SwiftUI)Array<Card>
)、遊戲邏輯 (func choose(card:)
)ViewModel R&R
EmojiMemoryGame
) 充當畫面資料提供的轉譯者Binding View & ViewModel
objectWillChange.send()
可以讓整個世界知道被改變了View R&R