CurrentValueSubject和@Published之间的区别 [英] Difference between CurrentValueSubject and @Published

查看:148
本文介绍了CurrentValueSubject和@Published之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在研究合并,并且出现了这个问题.

So I'm digging into combine and this question came up.

使用 CurrentValueSubject (并使用 currentValueSubject.value 设置其值)或使用 @Published var 和访问其发布商带有 $ ?我的意思是我知道一个人返回一个 Subject 而不是一个 Publisher ,但是我能找到的唯一真正的区别是 CurrentValueSubject 更加有用,因为您可以在协议上声明它.

Is there any real difference between using CurrentValueSubject (and setting its value using currentValueSubject.value) or using a @Published var and accessing its publisher with a $? I mean I know one returns a Subject instead of a Publisher, but the only real difference I could find is that CurrentValueSubject is way more useful because you can declare it on a protocol.

如果我们只能使用 PassthroughSubject ,我真的不明白 @Published 的用处,我在这里遗漏了什么吗?请记住,这是使用UIKit ,它可能还有SwiftUI的其他用途.

I really don't understand how @Published can be useful if we can just use PassthroughSubject, am I missing something here? Mind you, this is using UIKit, it may have other uses for SwiftUI.

谢谢.

推荐答案

@Published只是使用CurrentValueSubject更加整洁的一种快速方法.当我调试一个应用程序并查看$ paramName返回的类型时,它实际上只是一个CurrentValueSubject:

@Published is just a quick way to use CurrentValueSubject a little neater. When I debug one of my apps and look at the type returned by $paramName , it's actually just a CurrentValueSubject:

po self.$books
▿ Publisher
  ▿ subject : <CurrentValueSubject<Array<Book>, Never>: 0x6000034b8910>

我猜想使用CurrentValueSubject而不是@Published的好处可能是允许您使用错误类型?

I guess one benefit of using CurrentValueSubject instead of @Published may be to allow you to use the error type?

注意:尽管现在是CurrentValueSubject ,但我永远不会依赖该假设.

Note: Despite being a CurrentValueSubject right now I'd never rely on that assumption.

这篇关于CurrentValueSubject和@Published之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆