通用参数' A'无法推断(Swift 4,Bond和ReactiveKit框架从Swift 2迁移)

时间:2017-10-02 18:15:29

标签: swift migration swift4 reactivekit swiftbond

家伙!我试图从Swift 2迁移到Swift 4. Project正在使用Bond和ReactiveKit框架。我有UISearchBar的扩展名。所以我面临着从此迁移的问题:

import UIKit
import Bond
import ReactiveKit

public extension UISearchBar {
    public var bnd_delegate: ProtocolProxy {
        return protocolProxy(for: UISearchBarDelegate.self, setter: NSSelectorFromString("setDelegate:"))
    }
}

public extension UISearchBar {

    public var bnd_text: DynamicSubject<UISearchBar, String?> {
        let dispatcher: (PublishSubject<Void, NoError>, UISearchBar, String) -> Void = { _ in }
        let selector = #selector(UISearchBarDelegate.searchBar(_:textDidChange:))

    return DynamicSubject(
        target: self,
        signal: bnd_delegate.signal(for: selector, dispatch: dispatcher),
        get: { $0.text },
        set: { $0.text = $1 }
        )
    }
}

我已经做了一些更改并转到了这段代码:

import UIKit
import Bond
import ReactiveKit

public extension UISearchBar {
    public var delegate: ProtocolProxy {
    return protocolProxy(for: UISearchBarDelegate.self, setter: NSSelectorFromString("setDelegate:"))
}

public var text: DynamicSubject<String?> {
    let dispatcher: (PublishSubject<Void, NoError>, UISearchBar, String) -> Void = { _,_,_   in }
    let selector = #selector(UISearchBarDelegate.searchBar(_:textDidChange:))

    return DynamicSubject<String?>(
        target: self,
        signal: delegate.signal(for: selector, dispatch: dispatcher),
        get: { $0.text },
        set: { $0.text = $1 }
        )
    }
}

目前我在DynamicSubject上有错误: Generic parameter 'A' could not be inferred。这很奇怪,因为我检查了我发送的内容&#39;取值:(for: Selector, dispatch: (PublishSubject< S, NoError>, A, B) -> R)

我尝试使用多个变量进行调度,但什么都没得到。在我看来,Xcode并不能识别在PublishSubject参数之后开始的任何变量。

我想知道我做错了什么。

1 个答案:

答案 0 :(得分:1)

如果您从旧版本的ReactiveKit / Bond实现语法迁移,以下是如何为您的UIKit Reactive Extensions执行此操作:

import UIKit
import Bond
import ReactiveKit

extension ReactiveExtensions where Base: UISearchBar {

    var delegate: ProtocolProxy {
        return base.protocolProxy(for: UISearchBarDelegate.self, setter: NSSelectorFromString("setDelegate:"))
    }

    var text: DynamicSubject<String?> {

        let selector = #selector(UISearchBarDelegate.searchBar(_:textDidChange:))
        let textChanged = delegate.signal(for: selector) { (subj: SafePublishSubject<Void>, _: UISearchBar, _: NSString) in
            subj.next(())
        }

        return dynamicSubject(
            signal: textChanged,
            get: { $0.text },
            set: { $0.text = $1 }
        )
    }
}

extension UISearchBar: BindableProtocol {

    public func bind(signal: SafeSignal<String?>) -> Disposable {
        return reactive.text.bind(signal: signal)
    }
}