首页 > 解决方案 > '() -> 绑定目标' 不能转换为 '(Bool) -> BindingTarget'

问题描述

我最近继承了一个使用 ReactiveSwift 3.1.0 和 ReactiveCocoa 7.2.0 编写的 iOS 项目。我的任务是将它更新到 Swift 5 和最新的 ReactiveSwift 版本。

我将 ReactiveSwift 更新到 6.1.0,将 ReactiveCocoa 更新到 10.1.0。我不精通反应式编程,但我设法将现有代码库的很大一部分转换为最新版本。

但是我被困在这个特定的部分。原来的程序员不再可用。

在这个项目中,视图控制器上有一个扩展来显示错误消息。

extension Reactive where Base: UIViewController {
    func presentError(animated: Bool = true) -> BindingTarget<Error> {
        return self.makeBindingTarget { $0.present(error: $1, animated: animated) }
    }

    func present(error: Error, animated: Bool = true, completion: (() -> Void)? = nil) {
        LogError("Presenting error: \(error.verboseDescription)")
        self.present(UIAlertController(error: error, completion: completion), animated: animated, completion: nil)
    }
}

在视图控制器中,我在这一行收到以下错误。

self.reactive.presentError() <~ self.viewModel.reportAction.errors.map { $0 }

'() -> BindingTarget' 不能转换为 '(Bool) -> BindingTarget'

这是视图模型中的相关部分。

private(set) var reportAction: Action<(User, ReportReason), Void, APIKit.Error>!

这里发生的唯一变化是errors类型从viewModel.reportAction.errors.map { $0 }更改public let errors: Signal<Error, NoError>public let errors: Signal<Error, Never>。这是由于在最新的 ReactiveSwift 源代码中进行了一些更改。

这显然破坏了这段代码。我不确定为什么会发生错误以及如何修复这部分。

标签: iosswiftreactive-cocoareactive-swift

解决方案


推荐阅读