ObservableObject
Last updated
Was this helpful?
Last updated
Was this helpful?
⟩ ⟩ ObservableObject
When a conforms to the ObservableObject , any changes to its published values will cause all views using those values to automatically update, reflecting the changes.
在跟 CS193P 到第三課的時候,如果按照課程上的程式碼原封不動的照抄,程式可以正常執行。
有 Equatable 跟沒有 Equatable 怎麼差那麼多呢?SwiftUI 是如何判斷一個 view 到底要不要更新呢?
An observable object is a custom object (reference type) for your that can be bound to a view from storage in SwiftUI’s environment.
Use ObservableObject to declare an observable object .
Use to declare in an observable object type.
Use to initialize an observable object.
: any or object that uses the observable object's data.
:data shared with any views in your app.
environment object:an , which is put in the environment by calling an ancester view's modifier.
⟩ (protocol)
⟩ ⟩ (property wrapper)
SwiftUI Tutorials ⟩ ⟩
Sec. 4:
Sec. 5:
SwiftUI ⟩
Majid ⟩ #todo
但如果擅自將 Card 這個 struct 改成不僅遵循 同時也遵循 ,這時就出現問題了:「在按卡片的時候,卡片不會翻面」,這問題似乎跟 語法裡面,data
部分的 . 有關係。
就算 @Published var viewModel 更新了,但在 ForEach(viewModel.cards) 裡面的 cards 的 id 並沒有變更,再加上 Card 本身遵循 (這點很重要,因為如果沒有 ,程式可正常執行),造成 SwiftUI 判斷 EmojiMemoryGameView 的 body 裡面的 ForEach 部分不需更新,因此就不會翻面了。
⟩ ⟩
Swift ⟩ Standard Library ⟩ ⟩
- Hacking with Swift
- Hacking with Swift
- YouTube