Convert Proxy to Protocol

I have a class:

final class XpcMaker<T: Protocol>: NSObject	 
{
	private let exchangerProtocol: T
	
	init(exchangerProtocol: T)
	{
		self.exchangerProtocol = exchangerProtocol
		super.init()
	}

	func exchangeProxyAndConnection() -> (Any, NSXPCConnection)
	{
		let connection = NSXPCConnection(machServiceName: "... some name ...")
		connection.remoteObjectInterface = NSXPCInterface(with: exchangerProtocol.self) 
		let proxy =	connection.remoteObjectProxyWithErrorHandler()	{...}

		connection.resume()
		return (proxy, connection)
	}
}

I want to return proxy as T (instead of Any). I tried:

Cannot find type 'exchangerProtocol' in scope
guard let goodProxy = proxy as? exchangerProtocol else
guard let goodProxy = proxy as? exchangerProtocol.self else

'self' is not a member type of type 'T'
guard let goodProxy = proxy as? T.self else

This compiles, but always enters the else-branch at run time:
guard let goodProxy = proxy as? T else

So: How to return my proxy as T ?

That method returns “the proxy for the remote object (that is, the object exported from the other side of this connection)” and it sounds like that proxy doesn’t conform to your protocol. Have you looked in the debugger to see what’s being returned?

Why don't you just do this without having a maker that's hardly doing anything useful?

let connection = NSXPCConnection(machServiceName: "... some name ...")
connection.remoteObjectInterface = NSXPCInterface(with: YourInterface.self)
let proxy = connection.remoteObjectProxyWithErrorHandler { _ in /* ... */ }
    as! YourInterface
connection.resume()
1 Like

It returns:
proxy = __NSXPCInterfaceProxy_XpcTestClient.ClientToXpcExchangerProtocol
which is exactly what is should be.