Marina Marina - 4 months ago 8
iOS Question

Can not use proxyForObject function in DelegateProxyType (rxSwift)

I try to migrate delegate of SRWebSocket to observable. This is my RxSocketManagerDelegateProxy.swift:

class RxSocketManagerDelegateProxy: DelegateProxy, DelegateProxyType{

static func currentDelegateFor(object: AnyObject) -> AnyObject?{
let socket: SRWebSocket = object as! SRWebSocket
return socket.delegate
}

static func setCurrentDelegate(delegate: AnyObject?, toObject object: AnyObject) {
let socket: SRWebSocket = object as! SRWebSocket
socket.delegate = delegate as? SRWebSocketDelegate
}

}

extension SRWebSocket{
public var rx_delegate: DelegateProxy{
return DelegateProxyType.proxyForObject(self)
}

// ...

}


The problem is in proxyForObject function. It is not compiled like above. I get warning "Static member 'proxyForObject' cannot be used on instance of type 'DelegateProxyType.Protocol'".

When I try to use proxyForObjectFunction like this (though it is deprecated):

public var rx_delegate: DelegateProxy{
return proxyForObject(RxSocketManagerDelegateProxy.self, self)
}


I get message "assertion failed: : file /Users/Agentum/Documents/Xcode/Telemetry/Pods/RxCocoa/RxCocoa/Common/DelegateProxyType.swift".

How should I use proxyForObject() function in order to migrate the delegate correct?

Answer

You should be calling proxyForObject on RxSocketManagerDelegateProxy, not DelegateProxyType.

extension SRWebSocket {
    public var rx_delegate: DelegateProxy {
        return RxSocketManagerDelegateProxy.proxyForObject(self)
        // instead of: `return DelegateProxyType.proxyForObject(self)`
    }
}