Нужна помощь с переходом RxCocoa/RxSwift 3.6.1 на 4.5 (плюсом GMSMapView/GMSMapViewDelegate)

Нужна помощь в исправлении кода связанного с переходом от RxCocoa/RxSwift 3.6.1 на 4.5 (возможно дело не только в этих библиотеках), не могу понять где ошибка и что я делаю не так. Версия GoogleMaps (3.5.0) - эту библиотеку не менял.

Оригинал кода который был (не мой):

import GoogleMaps
import RxSwift
import RxCocoa

class RxGMSMapViewDelegateProxy: DelegateProxy, GMSMapViewDelegate, DelegateProxyType {
    //ошибка №1

    class func currentDelegateFor(_ object: AnyObject) -> AnyObject? {
        guard let mapView = object as? GMSMapView else { fatalError() }
        return mapView.delegate
    }

    class func setCurrentDelegate(_ delegate: AnyObject?, toObject object: AnyObject) {
        guard let mapView = object as? GMSMapView else { fatalError() }
        mapView.delegate = delegate as? GMSMapViewDelegate
    }

    override class func createProxyForObject(_ object: AnyObject) -> AnyObject {
        guard let mapView = object as? GMSMapView else { fatalError() }
        return mapView.createRxDelegateProxy()
    }
    //ошибка №2

    let didTapMarker = PublishSubject<GMSMarker>()

    func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {
        didTapMarker.onNext(marker)
        return true
    }

    deinit {
        didTapMarker.onCompleted()
    }
}

extension GMSMapView {

    func createRxDelegateProxy() -> RxGMSMapViewDelegateProxy {
        return RxGMSMapViewDelegateProxy(parentObject: self)
    }
    //ошибка №3
}

//swiftlint:disable force_cast
extension Reactive where Base: GMSMapView {

    var delegate: DelegateProxy {
        return RxGMSMapViewDelegateProxy.proxyForObject(base)
    }
    //ошибка №4

    var idleAtPosition: ControlEvent<GMSCameraPosition> {
        let source = delegate.methodInvoked(#selector(GMSMapViewDelegate.mapView(_:idleAt:)))
            .map { $0[1] as! GMSCameraPosition }

        return ControlEvent(events: source)
    }

    var didTapAtCoordinate: ControlEvent<CLLocationCoordinate2D> {
        let source = delegate.methodInvoked(#selector(GMSMapViewDelegate.mapView(_:didTapAt:)))
            .map { $0[1] as! CLLocationCoordinate2D }

        return ControlEvent(events: source)
    }

    var didTapMarker: ControlEvent<GMSMarker> {
        let source = (delegate as! RxGMSMapViewDelegateProxy).didTapMarker

        return ControlEvent(events: source)
    }
}

Перечислю ошибками на всякий случай:

  1. Cannot declare conformance to 'NSObjectProtocol' in Swift; 'RxGMSMapViewDelegateProxy' should inherit 'NSObject' instead. Reference to generic type 'DelegateProxy' requires arguments in <...> Insert '<AnyObject, Any>'. Type 'RxGMSMapViewDelegateProxy' does not conform to protocol 'DelegateProxyType' Do you want to add protocol stubs?

  2. Method does not override any method from its superclass

  3. Argument passed to call that takes no arguments

  4. Reference to generic type 'DelegateProxy' requires arguments in <...> Insert '<AnyObject, Any>'

Попробовал исправить все ошибки и получить вот такой код, который компилиться, но не работает, видно где-то я ошибка...

import GoogleMaps
import RxSwift
import RxCocoa

class RxGMSMapViewDelegateProxy: DelegateProxy<AnyObject, Any>, GMSMapViewDelegate, DelegateProxyType {
    
    static func registerKnownImplementations() {
        self.register(make: { $0 })
    }
    
    static func currentDelegate(for object: AnyObject) -> Any? {
        guard let mapView = object as? GMSMapView else { fatalError() }
        return mapView.delegate
    }
    
    static func setCurrentDelegate(_ delegate: Any?, to object: AnyObject) {
        guard let mapView = object as? GMSMapView else { fatalError() }
        mapView.delegate = delegate as? GMSMapViewDelegate
    }
    
//    непонятно нужна ли данная фуункция или какое у нее теперь имя должно быть
//    попробовал изменить на static func createProxy - она ниже
//    override class func createProxyForObject(_ object: AnyObject) -> AnyObject {
//        guard let mapView = object as? GMSMapView else { fatalError() }
//        return mapView.createRxDelegateProxy()
//    }
    
    static func createProxy(for object: AnyObject) -> RxGMSMapViewDelegateProxy {
        guard let mapView = object as? GMSMapView else { fatalError() }
        return mapView.createRxDelegateProxy()
    }

    let didTapMarker = PublishSubject<GMSMarker>()

    func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {
        didTapMarker.onNext(marker)
        return true
    }

    deinit {
        didTapMarker.onCompleted()
    }
}

extension GMSMapView {

    func createRxDelegateProxy() -> RxGMSMapViewDelegateProxy {
        return RxGMSMapViewDelegateProxy(parentObject: self, delegateProxy: RxGMSMapViewDelegateProxy.self)
    }
}

//swiftlint:disable force_cast
extension Reactive where Base: GMSMapView {

    var delegate: DelegateProxy<AnyObject, Any> {
        return RxGMSMapViewDelegateProxy.proxy(for: base)
    }

    var idleAtPosition: ControlEvent<GMSCameraPosition> {
        let source = delegate.methodInvoked(#selector(GMSMapViewDelegate.mapView(_:idleAt:)))
            .map { $0[1] as! GMSCameraPosition }

        return ControlEvent(events: source)
    }

    var didTapAtCoordinate: ControlEvent<CLLocationCoordinate2D> {
        let source = delegate.methodInvoked(#selector(GMSMapViewDelegate.mapView(_:didTapAt:)))
            .map { $0[1] as! CLLocationCoordinate2D }

        return ControlEvent(events: source)
    }

    var didTapMarker: ControlEvent<GMSMarker> {
        let source = (delegate as! RxGMSMapViewDelegateProxy).didTapMarker

        return ControlEvent(events: source)
    }
}

При использовании вылетает ошибка:

Fatal error: DelegateProxy has no factory of <GMSMapView: 0x7fecf945adf0; frame = (0 0; 0 0);

Помогите найти и исправить ошибку.


Ответы (0 шт):