gpt4 book ai didi

ios - 快速组合 : `append` which does not require output to be equal?

转载 作者:行者123 更新时间:2023-11-28 13:23:42 24 4
gpt4 key购买 nike

使用 Apple 的 Combine 我想在第一个发布者 foo 完成后附加一个发布者 bar(可以将 Failure 限制为 从不)。基本上我想要 RxJava's andThen .

我有这样的东西:

let foo: AnyPublisher<Fruit, Never> = /* actual publisher irrelevant */

let bar: AnyPublisher<Fruit, Never> = /* actual publisher irrelevant */

// A want to do concatenate `bar` to start producing elements
// only after `foo` has `finished`, and let's say I only care about the
// first element of `foo`.
let fooThenBar = foo.first()
.ignoreOutput()
.append(bar) // Compilation error: `Cannot convert value of type 'AnyPublisher<Fruit, Never>' to expected argument type 'Publishers.IgnoreOutput<Upstream>.Output' (aka 'Never')`

我想出了一个解决方案,我认为它可行,但它看起来非常丑陋/过于复杂。

let fooThenBar = foo.first()
.ignoreOutput()
.flatMap { _ in Empty<Fruit, Never>() }
.append(bar)

我是不是漏掉了什么?

编辑

在下面添加了我最初提案的更好版本作为答案。非常感谢@RobNapier!

最佳答案

我认为不是 ignoreOutput,您只想过滤所有项目,然后附加:

let fooThenBar = foo.first()
.filter { _ in false }
.append(bar)

您可能会发现重命名 dropAll() 更好:

extension Publisher {
func dropAll() -> Publishers.Filter<Self> { filter { _ in false } }
}

let fooThenBar = foo.first()
.dropAll()
.append(bar)

潜在的问题是 ignoreAll() 生成一个输出为 Never 的 Publisher,这通常是有道理的。但在这种情况下,您只想在不更改类型的情况下处理值,这就是过滤。

关于ios - 快速组合 : `append` which does not require output to be equal?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58718134/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com