我有一对实现 XPC 主应用程序 + 服务应用程序模式的 Swift Xcode 目标。我想出了如何来回传递自定义类,作为远程对象方法的参数或“返回值”(即完成处理程序/回复 block 的参数)。这一切都很好。
现在我正尝试添加一个新问题:使用来自服务的自定义类的 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
}
}
还有我在远程对象协议(protocol)上的方法签名:
@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)
但是,我显然没有完全正确的语法,而且我尝试过的许多语法变体也没有编译。谁能指出我正确的方向?
编辑:这是我遇到的运行时错误,让我相信我需要加入白名单:
<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]"
)}
最佳答案
在这种情况下你不需要白名单,这里是文档:
如果您发布的界面看起来像这样,则需要它
@objc func ReadAllSnapshotsRpc(then completion: @escaping (NSArray?, Error?) -> Void)
即如果您要返回一个未知的不透明数组,但是您返回明确类型化的对象,那么所有其他内容都在 NSSecureCoding
上,如所述。
更新:所述错误的原因在于解码 - 解码时应枚举所有类。以下是固定变体:
public required init?(coder: NSCoder) {
guard
let partials = coder.decodeObject(of: [NSArray.self, PartialSnapshot.self], forKey: "partials") as? [PartialSnapshot]
else {
NSLog("ReadAllSnapshotsRpcReturnType: returning nil from init?()")
return nil
}
self.partials = partials
}
关于swift - 如何在 Swift 中将 XPC 自定义类列入白名单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72710588/