Home > Software design >  Selector type of expression is ambiguous without more context
Selector type of expression is ambiguous without more context

Time:10-06

I'm trying to get ARSessionDelegate method using selector, but im getting this error: Type of expression is ambiguous without more context

There is mine code:

#selector(ARSessionDelegate.session(_:didUpdate:) as ((ARSessionDelegate) -> (ARSession, ARFrame) -> Void)) 

That's how this method looks like:

public protocol ARSessionDelegate : ARSessionObserver {
    optional func session(_ session: ARSession, didUpdate frame: ARFrame)
}

And also, I am trying to make an rx extension for the ARKit session using this answer, but im not sure it's caused the problem.

CodePudding user response:

Because there are multiple methods with the same selector name, you are forced to implement the method in the delegate and forward the calls using a subject. Like this:

extension ARSession: HasDelegate { }

extension Reactive where Base: ARSession {
    var delegate: ARSessionDelegateProxy {
        return ARSessionDelegateProxy.proxy(for: base)
    }

    var didUpdate: Observable<ARFrame> {
        return delegate.didUpdate.asObservable()
    }
}

final class ARSessionDelegateProxy
: DelegateProxy<ARSession, ARSessionDelegate>
, DelegateProxyType
, ARSessionDelegate {
    init(parentObject: ARSession) {
        super.init(
            parentObject: parentObject,
            delegateProxy: ARSessionDelegateProxy.self
        )
    }

    deinit {
        didUpdate.onCompleted()
    }

    public static func registerKnownImplementations() {
        self.register { ARSessionDelegateProxy(parentObject: $0) }
    }

    func session(_ session: ARSession, didUpdate frame: ARFrame) {
        didUpdate.onNext(frame)
    }

    fileprivate let didUpdate = PublishSubject<ARFrame>()
}
  • Related