Swift Combine:如何将 `AnyPublisher<[Foo], *>` 转换为 `AnyPublisher<Foo, *>`?

Saj*_*jon 3 swift rx-swift combine

如何将数组某个元素的发布者转换为该元素的发布者(但有更多事件)?

例如,我该如何转换

AnyPublisher<[Int], Never>AnyPublisher<Int, Never>

我想也许 RxSwift 提供的from操作符与我想要做的类似。

我想我想要 Combine 的倒数collect

Dan*_* T. 5

这是代码:

func example(publisher: AnyPublisher<[Foo], Never>) -> AnyPublisher<Foo, Never> {
    return publisher
        .map { $0.publisher }
        .switchToLatest()
        .eraseToAnyPublisher()
}
Run Code Online (Sandbox Code Playgroud)