如何创建可以具有任何类型输出的 PassthroughSubject?
我想创建一个可以发送任何类型的输出的 PassthroughSubject 对象。在代码中,我目前有这样的内容:
let cmd1Subj = PassthroughSubject<String, Never>()
let cmd2Subj = PassthroughSubject<String, Never>()
var desiredCmd: PassthroughSubject<String, Never>?
let executeDesiredCmdSubj = PassthroughSubject<String, Never>()
var arg: String?
func executeDesiredCmd(cmdArg: String) -> AnyPublisher<String, Never> {
guard (desiredCmd != nil) else {
return Just("Nothing to execute\n").eraseToAnyPublisher()
}
desiredCmd?.send(cmdArg)
return Just("Executed: \(String(describing: desiredCmd)) with argument: \(cmdArg)").eraseToAnyPublisher()
}
let cancellable = executeDesiredCmdSubj
.flatMap(executeDesiredCmd)
.receive(on: DispatchQueue.main)
.sink(receiveValue: {
print($0)
})
desiredCmd = cmd1Subj
arg = "This is the argument for command 1"
desiredCmd?.send(arg!)
desiredCmd = cmd2Subj
arg = "This is the argument for command 2"
desiredCmd?.send(arg!)
如何更改 desiredCmd
和 executeDesiredCmdSubj
以便它们可以发送运行时确定的任何类型的输出?我想做这样的事情:
let cmd1Subj = PassthroughSubject<Int, Never>()
let cmd2Subj = PassthroughSubject<String, Never>()
var desiredCmd: PassthroughSubject<Some_Generic_Type, Never>?
let executeDesiredCmdSubj = PassthroughSubject<Some_Generic_Type, Never>()
var arg: Some_Generic_Type?
func executeDesiredCmd(cmdArg: Some_Generic_Type) -> AnyPublisher<String, Never> {
guard (desiredCmd != nil) else {
return Just("Nothing to execute\n").eraseToAnyPublisher()
}
desiredCmd?.send(cmdArg)
return Just("Executed: \(String(describing: desiredCmd)) with argument: \(cmdArg)").eraseToAnyPublisher()
}
let cancellable = executeDesiredCmdSubj
.flatMap(executeDesiredCmd)
.receive(on: DispatchQueue.main)
.sink(receiveValue: {
print($0)
})
desiredCmd = cmd1Subj
arg = 12345
desiredCmd?.send(arg!)
desiredCmd = cmd2Subj
arg = "This is the argument for command 2"
desiredCmd?.send(arg!)
其中 Some_Generic_Type 是一个占位符,可用于表示我尝试通过 PassthroughSubject 传递的任何类型。我尝试使用“Any”作为占位符,但它产生了几个编译错误:
- “无法分配类型 'PassthroughSubject
' 的值”输入“PassthroughSubject<任何,从不>” - “无法分配“PassthroughSubject
”类型的值输入“PassthroughSubject<任何,从不>”
I would like to create a PassthroughSubject object that can send an output of any type. In code I currently have something like this:
let cmd1Subj = PassthroughSubject<String, Never>()
let cmd2Subj = PassthroughSubject<String, Never>()
var desiredCmd: PassthroughSubject<String, Never>?
let executeDesiredCmdSubj = PassthroughSubject<String, Never>()
var arg: String?
func executeDesiredCmd(cmdArg: String) -> AnyPublisher<String, Never> {
guard (desiredCmd != nil) else {
return Just("Nothing to execute\n").eraseToAnyPublisher()
}
desiredCmd?.send(cmdArg)
return Just("Executed: \(String(describing: desiredCmd)) with argument: \(cmdArg)").eraseToAnyPublisher()
}
let cancellable = executeDesiredCmdSubj
.flatMap(executeDesiredCmd)
.receive(on: DispatchQueue.main)
.sink(receiveValue: {
print($0)
})
desiredCmd = cmd1Subj
arg = "This is the argument for command 1"
desiredCmd?.send(arg!)
desiredCmd = cmd2Subj
arg = "This is the argument for command 2"
desiredCmd?.send(arg!)
How do I change desiredCmd
and executeDesiredCmdSubj
such that they can send an output of any type, as determined at runtime? I'd like to do something like this:
let cmd1Subj = PassthroughSubject<Int, Never>()
let cmd2Subj = PassthroughSubject<String, Never>()
var desiredCmd: PassthroughSubject<Some_Generic_Type, Never>?
let executeDesiredCmdSubj = PassthroughSubject<Some_Generic_Type, Never>()
var arg: Some_Generic_Type?
func executeDesiredCmd(cmdArg: Some_Generic_Type) -> AnyPublisher<String, Never> {
guard (desiredCmd != nil) else {
return Just("Nothing to execute\n").eraseToAnyPublisher()
}
desiredCmd?.send(cmdArg)
return Just("Executed: \(String(describing: desiredCmd)) with argument: \(cmdArg)").eraseToAnyPublisher()
}
let cancellable = executeDesiredCmdSubj
.flatMap(executeDesiredCmd)
.receive(on: DispatchQueue.main)
.sink(receiveValue: {
print($0)
})
desiredCmd = cmd1Subj
arg = 12345
desiredCmd?.send(arg!)
desiredCmd = cmd2Subj
arg = "This is the argument for command 2"
desiredCmd?.send(arg!)
where Some_Generic_Type is a placeholder that can be used to represent any type I attempt to pass through my PassthroughSubject. I tried using "Any" as the placeholder but it produces a couple of compilation errors:
- "Cannot assign value of type 'PassthroughSubject<Int, Never>' to type 'PassthroughSubject<Any, Never>"
- "Cannot assign value of type 'PassthroughSubject<String, Never>' to type 'PassthroughSubject<Any, Never>"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我可以通过创建一个定义几个具有关联值的枚举的结构来做到这一点:
I was able to do this by creating a struct that defines a couple of enums with associated values: