我有一对Swift Xcode目标,它们实现了XPC主应用程序+服务应用程序模式.我了解了如何来回传递自定义类,可以作为远程对象方法的参数,也可以作为"返回值"(即,完成处理程序/应答块的参数).这一切都很好.
现在,我正在try 添加一个新的折痕:使用服务中我的自定义类的NSArray调用完成处理程序,以将它们返回到主应用程序.我从Apple's docs(和this Obj-C answer)了解到,我需要在接收端的NSXPCInterface
上将我的自定义类列入白名单.但我不知道如何在Swift 做到这一点.所有在线和文档中的示例都在Obj-C中,我很难在Swift中找到正确的对等项.
具体来说,这里是我的自定义类的简化版本:
@objc(PartialSnapshot) public class PartialSnapshot : NSObject, NSSecureCoding {
public var rawPaths: [String]
public static var supportsSecureCoding: Bool = true
public func encode(with coder: NSCoder) {
coder.encode(rawPaths, forKey: "rawPaths")
}
public required init?(coder: NSCoder) {
guard
let rawPaths = coder.decodeObject(of: [NSArray.self], forKey: "rawPaths") as? [String]
else {
NSLog("PartialSnapshot: returning nil from init?(coder: NSCoder)")
return nil
}
self.rawPaths = rawPaths
}
}
@objc(ReadAllSnapshotsRpcReturnType) public class ReadAllSnapshotsRpcReturnType : NSObject, NSSecureCoding {
var partials: [PartialSnapshot]
public static var supportsSecureCoding: Bool = true
public func encode(with coder: NSCoder) {
coder.encode(partials, forKey: "partials")
}
public required init?(coder: NSCoder) {
guard
let partials = coder.decodeObject(of: [NSArray.self], forKey: "partials") as? [PartialSnapshot]
else {
NSLog("ReadAllSnapshotsRpcReturnType: returning nil from init?()")
return nil
}
self.partials = partials
}
}
远程对象协议上的我的方法签名:
@objc func ReadAllSnapshotsRpc(then completion: @escaping (ReadAllSnapshotsRpcReturnType?, Error?) -> Void)
我想我大概知道从this Objective-C answer开始我需要做什么.当我在主应用程序中创建我的NSXPCConnection
时,我需要添加一行,如下面的第三行:
let connection = NSXPCConnection(machServiceName: SecurityProxyConstants.domain, options: .privileged)
connection.remoteObjectInterface = NSXPCInterface(with: SecurityProxyProtocol.self)
connection.remoteObjectInterface?.setClasses(ReadAllSnapshotsRpcReturnType.self, for: #selector(PartialSnapshot.self), argumentIndex: 0, ofReply: true)
然而,我显然没有完全正确的语法,我try 过的许多语法变体也没有编译.有人能给我指出正确的方向吗?
EDIT:这是我遇到的运行时错误,这让我相信我需要列入白名单:
<NSXPCConnection: 0x600003f5b0c0> connection to service with pid 9679 named com.mycompany.MyApp.SecurityProxy: Exception caught during decoding of reply to message 'ReadAllSnapshotsRpcWithThen:', dropping incoming message and calling failure block.
Ignored Exception: Exception while decoding argument 0 (#1 of invocation):
<NSInvocation: 0x600001bdb740>
return value: {v} void
target: {@?} 0x0 (block)
argument 1: {@} 0x0
argument 2: {@} 0x0
Exception: value for key 'NS.objects' was of unexpected class 'PartialSnapshot' (0x10c7abe00) [/Users/mwg/Library/Developer/Xcode/DerivedData/MyApp-brumjpaxvoxfmjchpswlfnbsarho/Build/Products/Debug/MyApp.app].
Allowed classes are:
{(
"'NSArray' (0x7ff8485798a0) [/System/Library/Frameworks/CoreFoundation.framework]"
)}