Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift CurrentValueSubject的订阅服务器如何捕获错误_Swift_Combine - Fatal编程技术网

Swift CurrentValueSubject的订阅服务器如何捕获错误

Swift CurrentValueSubject的订阅服务器如何捕获错误,swift,combine,Swift,Combine,我正在使用CurrentValueSubject填充diffabledatasource表 我怎样才能抓住错误 var strings=CurrentValueSubject([String]()) 现在receiveCompletion接收到错误,但提到使用.catch,但我看不出这种方法在这种情况下是否有效?您可以使用.catch基本上用一个有效的[String]替换错误(在这种情况下可能是一个空数组): 补充阅读: viewModel.strings .receive(on: Di

我正在使用
CurrentValueSubject
填充diffabledatasource表

我怎样才能抓住错误

var strings=CurrentValueSubject([String]())


现在receiveCompletion接收到错误,但提到使用
.catch
,但我看不出这种方法在这种情况下是否有效?

您可以使用
.catch
基本上用一个有效的
[String]
替换
错误(在这种情况下可能是一个空数组):

补充阅读:

viewModel.strings
    .receive(on: DispatchQueue.main)
    .sink(receiveCompletion: {
        print("completion \($0)")
    }, receiveValue: { [weak self] in
        self?.applySnapshot()
    })
    .store(in: &cancellables)
.receive(on: DispatchQueue.main)
            .catch { error -> Just<[String]> in
                print(error)
                return Just([])
            }
            .sink(receiveValue: { [weak self] _ in
                self?.applySnapshot()
            })
            .store(in: &cancellables)
.receive(on: DispatchQueue.main)
            .replaceError(with: [])
            .sink(receiveValue: { [weak self] _ in
                self?.applySnapshot()
            })
            .store(in: &cancellables)