gpt4 book ai didi

xcode - NSXPCInterface 构造函数无法识别 Swift 中的协议(protocol)

转载 作者:行者123 更新时间:2023-12-02 16:02:02 34 4
gpt4 key购买 nike

我正在快速创建 XPC 服务,并且创建了我的协议(protocol):

protocol MyProtocol {

func myFunc()

}

当我尝试通过使用协议(protocol)初始化 NSXPCInterface 的新对象来设置导出对象实现的接口(interface)(在我的 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”(又名“MyProtocol.Protocol”)的值转换为预期参数类型“Protocol”

谁能帮我解决这个错误吗?

最佳答案

要引用协议(protocol)的类型,您需要在其上使用 .self:

 newConnection.exportedInterface = NSXPCInterface(withProtocol: MyProtocol.self)

您还必须将 @objc 添加到协议(protocol)声明中:

@objc protocol MyProtocol {
// ...
}

关于xcode - NSXPCInterface 构造函数无法识别 Swift 中的协议(protocol),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33480712/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com