gpt4 book ai didi

swift - macOS XPC 服务未启动

转载 作者:搜寻专家 更新时间:2023-11-01 06:38:31 24 4
gpt4 key购买 nike

我有一个桌面 Swift 应用程序,其中包含一个已停止工作的 XPC 服务。从那以后我设法让它工作了一次,但它又被打破了。我尝试将代码简化为最简单的情况,但感觉有一个因素我不明白。

初始化代码:

connection = NSXPCConnection(serviceName: XPCBundleID)
connection.remoteObjectInterface = NSXPCInterface(withProtocol:UploadServiceProtocol.self)
connection.resume()

测试函数:

func initService(){
let uploaderRemoteObject = connection.remoteObjectProxyWithErrorHandler() {
(error) in NSLog("UploadServiceClient - remote proxy error: %@", error)
} as! UploadServiceProtocol
uploaderRemoteObject.initService?(nil) { (data) in
NSLog("UploadServiceClient - initService - Got \(data?.length) bytes.")
}

}

如果我打印 uploaderRemoteObject我得到 <_NSXPCDistantObjectWithError: 0x618000083980>这可能是正常的,但实际的 XPC 进程永远不会启动(当然也不会调用远程方法)。

服务协议(protocol)是:

@objc(UploadServiceProtocol) protocol UploadServiceProtocol {
optional func initService(action:String?, withReply: (NSData?)->Void )
}

XPC main.swift 的内容是:

class ServiceDelegate: NSObject, NSXPCListenerDelegate {
func listener(listener: NSXPCListener, shouldAcceptNewConnection newConnection: NSXPCConnection) -> Bool {
newConnection.exportedInterface = NSXPCInterface(withProtocol: UploadServiceProtocol.self)
newConnection.exportedObject = ImageUploader()
newConnection.resume()
return true
}
}
let delegate = ServiceDelegate()
let listener = NSXPCListener.serviceListener()
listener.delegate = delegate;
listener.resume()

请注意,我确信 XPCBundleID是正确的,我正在为 Mac App Store 签署所有内容。有什么想法吗?

最佳答案

解决了:显然你不能在 XPC 远程协议(protocol)中有可选函数。从 UploadServiceProtocol 中删除 optional 修复了它。

关于swift - macOS XPC 服务未启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38493035/

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