我正在快速创建XPC服务,并且创建了协议:
protocol MyProtocol {
func myFunc()
}
当我尝试通过协议初始化NSXPCInterface的新对象来设置导出对象实现的接口(在main.swift中)时,出现错误:
/// This method is where the NSXPCListener configures, accepts, and resumes a new incoming NSXPCConnection.
func listener(listener: NSXPCListener, shouldAcceptNewConnection newConnection: NSXPCConnection) -> Bool {
// Configure the connection.
// First, set the interface that the exported object implements.
newConnection.exportedInterface = NSXPCInterface(MyProtocol)
错误是:无法将类型'((MyProtocol).Protocol'(aka'MyProtocol.Protocol')类型的值转换为预期的参数类型'Protocol'
谁能帮我解决这个错误?
最佳答案
要引用协议的类型,您需要在其上使用.self
:
newConnection.exportedInterface = NSXPCInterface(withProtocol: MyProtocol.self)
您还必须在协议声明中添加
@objc
:@objc protocol MyProtocol {
// ...
}