Generic Protocol

I have a class, which uses a specific Protocol in several places. Like:

let connection.remoteObjectInterface = NSXPCInterface(with: MySpecialProtocol.self)


let aProxy = connection.remoteObjectProxy as? MySpecialProtocol
private var currentProxy: MySpecialProtocol?

I would like to use this class also in other programs - with obviously some other Protocol.

I tried:

final class TestHandler  
	let SomeProtocol: Protocol
	init(protocol someProtocol: Protocol)
		SomeProtocol = someProtocol

	func test() -> Any  
		let connection = NSXPCConnection(serviceName: "machServiceName" )
		connection.remoteObjectInterface = NSXPCInterface(with: SomeProtocol.self)
		let aProxy = connectionToService.remoteObjectProxy
		//	Error: Cannot find type 'SomeProtocol' in scope
		let bProxy = connectionToService.remoteObjectProxy as? SomeProtocol

		return aProxy

Any idea how to fix this? Also the function test() should return not "Any" but "SomeProtocol".
Maybe make the class generic about the protocol? But how?


Did you mean to write?

final class TestHandler  {
	let someProtocol: SomeProtocol
	init (protocol: SomeProtocol) {
		someProtocol = protocol