gpt4 book ai didi

ios - `flatMap` 发布者有不同的错误

转载 作者:行者123 更新时间:2023-12-01 16:05:12 24 4
gpt4 key购买 nike

我有一个像下面这样的 Swift 发布者

let item = PassthrogutSubject<String, Never>()
收到字符串后,我想发出网络请求
为此,我正在使用 flatMap我的网络请求签名如下所示:
func loadDetails(_ code: String) -> AnyPublisher<CustomType, ErrorAlert>
我怎样才能做到以下几点:
$item
.filter { !$0.isEmpty }
.flatMap { [unowned self] (string) in
self.viewModel.loadDetails(string) }
.receive(on: DispatchQueue.main)
.sink(receiveCompletion: { _ in
...
}) { ... }
.store(in: &subscriptions)
错误: Instance method 'flatMap(maxPublishers:_:)' requires the types 'Published<Value>.Publisher.Failure' (aka 'Never') and 'Alert' be equivalent

最佳答案

在我的问题进行到一半时,我在接收第一个订户时与以下运算符(operator)一起解决了这个问题

$item
.filter { !$0.isEmpty }
.setFailureType(to: ErrorAlert.self) // <---- 💯 HERE
.flatMap { [unowned self] (string) in
self.viewModel.loadDetails(string) }
.receive(on: DispatchQueue.main)
.sink(receiveCompletion: { _ in
...
}) { ... }
.store(in: &subscriptions)

关于ios - `flatMap` 发布者有不同的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63307322/

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