RxSwift:调用中的额外参数 'onError'

RxSwift: Extra argument 'onError' in call

我在 Swift 4 中有以下代码与 RxSwift

worthReacting.flatMap{ (userSearch) in
        translator.getTranslation(ofWord: userSearch)
        }.subscribe(
            onSuccess: {(dataModel) in
                state.value = .translation(word: dataModel.definition,
                                           translations: dataModel.translations)
        },
            onError: {(error) in
                state.value = .networkError
        },
            onCompleted: {
                state.value = .unknownWord
        }).disposed(by: disposeBag)

worthReacting 的类型为 Observable<String>
translator.getTranslation returns Maybe<DataModel>

我遇到构建错误

Extra argument 'onError' in call

Maybe 平面映射到 Observable 生成 ObservableObservable 无法发出 onSuccess 事件,而是会发出 onNext。以下代码将起作用:

worthReacting.flatMap{ (userSearch) in
        translator.getTranslation(ofWord: userSearch)
        }.subscribe(
            onNext: {(dataModel) in
                self.state.value = .translation(word: dataModel.definition,
                                           translations: dataModel.translations)
        },
            onError: {(error) in
                self.state.value = .networkError
        },
            onCompleted: {
                self.state.value = .unknownWord
        }).disposed(by: disposeBag)

对于那些收到 OP 错误但有不同原因的人,请检查您是否没有通过不使 do-catch 子句详尽无遗来偶然关闭其中一个 throwing

此外,请注意不要使用单语句闭包(在这种情况下,您应该明确 return)。