跳过导航
I have come across something with the Observation framework that is confusing me so if there are any…
0:00 / 0:52

I have come across something with the Observation framework that is confusing me so if there are any…

StewartLynch 🇨🇦

@StewartLynch1.2万 位关注者

在 X 上查看
36 个赞下载原帖

10,172 次观看 #SwiftUI

I have come across something with the Observation framework that is confusing me so if there are any #SwiftUI Observation framework experts out there that can explain this to me, I would be forever grateful. Why I don’t have to define the object as a State object?

8 条评论

评论来自 X (Twitter) 上的原帖

  • Maxim Bazarov 的头像

    @bazar4tw

    That’s because SwiftUI automatically tracks any observable properties that a view’s body reads directly.

    7 个赞

  • StewartLynch 🇨🇦 的头像

    @StewartLynch

    Thanks Maxim. Not sure how I missed that.

    3 个赞

  • Parsa 的头像

    @ParsaNasirimehr

    The first code's equivalent with the old combine stuff is marking the MyObject as an ObservedObject. This binds it to the lifecycle of the struct, meaning that the property changes retrigger the body, and any re-invocation of the ContentView itself would create a new MyObject. It's due to how the new Observable grabs references of the component that is listening to a certain keypath (\.value in this case) and notifies them of the change.

    3 个赞

  • Igor Tarasenko 的头像

    @sa1k0s

    Expand the macro. This will help in your understanding.

    3 个赞

  • DSWH 的头像

    @ITS_A_NERD

    The macro sets state. The protocol expects you to set state

    2 个赞

  • Andres Raigoza 👨‍💻 的头像

    @andresr_develop

    This shows how efficient the macro is compared to ObservableObject, it runs the body but it doesn’t recreate the View

    1 个赞

  • DSWH 的头像

    @ITS_A_NERD

    The macro is new. I’m still working on a m1 :(. Older Xcode.

  • DART Meadow 的头像

    @DARTMeadow

    I think the increment operator syntax: += , is colliding -for itself as a variable in the production code below the observable code block, you might need additional syntax for the observable API.