我正在尝试在withReply
签名上使用我自己的类型编写 XPC 服务。类型/类具有 Xcode 的主应用程序和 XPC 服务的“目标成员资格”。但是incompatible reply block signature
,即使withReply
签名中使用了相同的类,我也会进入调试输出,但是 Xcode 目标不同,我将在下面解释。
注意:这是在 Swift 中使用这个项目来完成的。除了那里他们使用NSData
而不是自定义类型。
细节
出于这个问题的目的,我将使用以下示例
- 自定义类 -
Tweet
- 该类符合NSSecureCoding
协议,以便在主应用程序和 XPC 服务之间传递 - XPC 协议 -
TweetTransfer
需要一种方法func take(_ count: Int, withReply: ((Tweet) -> Void))
然后是我导出符合TweetTransfer
. XPC 服务似乎已启动,但随后在它与主应用程序之间传输失败并出现
XPCWorker[11069:402719] <NSXPCConnection: 0x61800010e220> connection from pid 11066 received an undecodable message
完整的消息如下[1],但“wire”和“local”之间的唯一区别是参数一是
- 金属丝 -
_TtC17MainApp5Tweet
- 当地的 -
_TtC23XPCWorker5Tweet
Xcode 目标不同的地方。这足以扔掉它吗?那么我如何在应用程序和它的 XPC 服务之间共享代码呢?
[1] 完整的错误文本
<NSXPCConnection: 0x61800010e220> connection from pid 11066 received an undecodable message (incompatible reply block signature (wire: <NSMethodSignature: 0x618000074ec0>
number of arguments = 2
frame size = 224
is special struct return? NO
return value: -------- -------- -------- --------
type encoding (v) 'v'
flags {}
modifiers {}
frame {offset = 0, offset adjust = 0, size = 0, size adjust = 0}
memory {offset = 0, size = 0}
argument 0: -------- -------- -------- --------
type encoding (@) '@?'
flags {isObject, isBlock}
modifiers {}
frame {offset = 0, offset adjust = 0, size = 8, size adjust = 0}
memory {offset = 0, size = 8}
argument 1: -------- -------- -------- --------
type encoding (@) '@"_TtC17MainApp5Tweet"'
flags {isObject}
modifiers {}
frame {offset = 8, offset adjust = 0, size = 8, size adjust = 0}
memory {offset = 0, size = 8}
class '_TtC17MainApp5Tweet'
vs local: <NSMethodSignature: 0x610000074740>
number of arguments = 2
frame size = 224
is special struct return? NO
return value: -------- -------- -------- --------
type encoding (v) 'v'
flags {}
modifiers {}
frame {offset = 0, offset adjust = 0, size = 0, size adjust = 0}
memory {offset = 0, size = 0}
argument 0: -------- -------- -------- --------
type encoding (@) '@?'
flags {isObject, isBlock}
modifiers {}
frame {offset = 0, offset adjust = 0, size = 8, size adjust = 0}
memory {offset = 0, size = 8}
argument 1: -------- -------- -------- --------
type encoding (@) '@"_TtC23XPCWorker5Tweet"'
flags {isObject}
modifiers {}
frame {offset = 8, offset adjust = 0, size = 8, size adjust = 0}
memory {offset = 0, size = 8}
class '_TtC23XPCWorker5Tweet'
)
更新
有关协议、remoteObjectProxy 连接和 Tweet 对象的更多信息。这是用于 XPC 调用的协议:
@objc(TweetTransfer)
protocol TweetTransfer {
func take(_ count: Int, withReply: replyType)
}
typealias replyType = ((Tweet) -> Void)
为方便起见,我使用了类型别名。然后Tweet
对象非常简单,仅用于测试(虽然支持 NSSecureCoding 有点复杂):
final class Tweet: NSObject, NSSecureCoding {
var name: String
var text: String
static var supportsSecureCoding = true
init(name: String, text: String) {
self.name = name
self.text = text
}
init?(coder aDecoder: NSCoder) {
guard let name = aDecoder.decodeObject(forKey: "name") as? String else {
fatalError("Could not deserialise name!")
}
guard let text = aDecoder.decodeObject(forKey: "text") as? String else {
fatalError("Could not deseralise text!")
}
self.name = name
self.text = text
}
func encode(with aCoder: NSCoder) {
aCoder.encode(name, forKey: "name")
aCoder.encode(text, forKey: "text")
}
}
最后是我们调用 remoteObjectProxy 的地方
guard let loader = workerConnection.remoteObjectProxyWithErrorHandler(handler) as? TweetTransfer else {
fatalError("Could not map worker to TweetTransfer protocol!")
}
var tweets = [Tweet]()
loader.take(1) { tweet in
tweets.append(tweet)
}