@AppStorage Inside observableObject-如何从属性包装器内部触发`objectWillChange`?

发布于 2025-01-30 10:41:52 字数 747 浏览 4 评论 0 原文

在iOS 14.5中,Apple更改为 @AppStorage ,以便可以在 observableObject 中正确使用它:

AppStorage属性包装器现在按预期运行时可以使用可观察到的对象,从而导致系统发射ObjectWillChange Publisher。 (65562845)

  • 资料来源:

他们如何设法调用 observableboceboceboce ' ObjectWillChange 发布者从 @AppStorage 属性包装器中发行?他们在使用一些内部API吗?还是可以使用任何物业包装器来做到这一点?

我知道“魔术”的一部分是 dynamicproperty 协议。但是,仅此一项就不足以触发 objectWillChange 事件。我的猜测是,这是它们用于 @publed 的逻辑,据我所知,现在也不能真正成为“储备工程”。

有什么想法吗?

谢谢!

In iOS 14.5, Apple made a change to @AppStorage so that it can be properly used within an ObservableObject:

AppStorage property wrappers now work as expected when contained inside an ObservableObject, causing the system to emit the objectWillChange publisher. (65562845)

How do they manage to call the ObservableObject's objectWillChange publisher from within the @AppStorage property wrapper? Are are they using some internal API? Or is it possible to do that with any property wrapper?

I know that one part of this "magic" is the DynamicProperty protocol. But that alone is not enough to trigger an objectWillChange event. My guess would be that it's the same logic they used for @Published, which - as far as I know - also can't really be "reserve engineered" right now.

Any ideas?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

从此见与不见 2025-02-06 10:41:52

上次我检查时,有一个类扩展名,可以修改类,将调用添加到 objectwillchange.send() of @publed 属性和我也假设 @AppStorage 属性。我还记得如果您自己定义 abocew> absockwillchange 自己会关闭这些机制。当我对其进行逆向工程时,代码对我来说看起来很脆弱,我确实认为自己只会调用 objectwillsend 我自己更安全。

Last time I checked there was a class extension that modifies the class to add the call to objectWillChange.send() in the willSet of @Published properties and I assume also @AppStorage properties. I remember also if you define objectWillChange yourself then these mechanics get turned off. When I reverse engineered it, the code looked very fragile to me and I did think it maybe safer to just call objectWillSend myself.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文