簡體   English   中英

'() -&gt; 綁定目標<error> ' 不能轉換為 '(Bool) -&gt; BindingTarget<error> '</error></error>

[英]'() -> BindingTarget<Error>' is not convertible to '(Bool) -> BindingTarget<Error>'

我最近繼承了一個使用 ReactiveSwift 3.1.0 和 ReactiveCocoa 7.2.0 編寫的 iOS 項目。 我的任務是將其更新到 Swift 5 和最新的 ReactiveSwift 版本。

我將 ReactiveSwift 更新到 6.1.0,將 ReactiveCocoa 更新到 10.1.0。 我不精通反應式編程,但我設法將現有代碼庫的很大一部分轉換為最新版本。

但是我被困在這個特定的部分。 原來的程序員不再可用。

在這個項目中,視圖 controller 有一個擴展來顯示錯誤消息。

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)
    }
}

在 controller 視圖中,我在此行收到以下錯誤。

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

'() -> BindingTarget' 不能轉換為 '(Bool) -> BindingTarget'

這是視圖 model 中的相關部分。

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

這里發生的唯一變化是viewModel.reportAction.errors.map { $0 }中的errors類型從public let errors: Signal<Error, NoError>更改為public let errors: Signal<Error, Never> 這是由於在最新的 ReactiveSwift 源代碼中進行了一些更改

這顯然破壞了這段代碼。 我不確定為什么會發生錯誤以及如何修復這部分。

我不認為目前的錯誤方法應該在反應擴展中,而是在 UIViewController 的擴展中。 因為綁定目標閉包中的第一個參數將是Base的任何類型。 所以…

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

extension UIViewController {

    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)
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM