Search code examples
swiftmigrationswift4reactivekitswiftbond

Generic parameter 'A' could not be inferred (Swift 4, Bond and ReactiveKit frameworks migrating from Swift 2)


guys! I'm trying to migrate from Swift 2 to Swift 4. Project is using Bond and ReactiveKit frameworks. I have extension for UISearchBar. So I'm facing a problem migrating from this:

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

I already made some changes and become to this code:

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

Currently I have error with DynamicSubject: Generic parameter 'A' could not be inferred. And it is strange, because I checked what does my 'dispatch' takes as a value: (for: Selector, dispatch: (PublishSubject< S, NoError>, A, B) -> R).

I've tried to play with number of variables for dispatch, but get nothing. It seems to me Xcode doesn't recognize any variable starting after PublishSubject argument.

I want to know what I'm doing wrong.


Solution

  • If you ever migrated from old versions of ReactiveKit/Bond implemented syntax, here is how to do it for your 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)
        }
    }