我有一对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]"
)}

推荐答案

在这种情况下,您不需要白名单,以下是文档:

doc

如果发布的界面看起来像

@objc func ReadAllSnapshotsRpc(then completion: @escaping (NSArray?, Error?) -> Void)

i、 如果返回一个未知内容的不透明数组,但返 echo 式类型的对象,那么其他所有内容都是NSSecureCoding.

Update:所述错误的原因是在解码中-对于解码,应枚举所有类.以下是固定变量:

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相关问答推荐

如何在SwiftUI中创建具有圆角顶部和锯齿状底部边缘的自定义Shape,类似于撕破的纸?

在Swift中initt()完成后运行一个函数

字符串目录不适用于SWIFT包

SWIFT:使字典数组值可变

解码器是否需要具有密钥的外部数据表示?

如何在 Vapor 中制作可选的查询过滤器

Swift 运算符中 inout 的行为

deinitialize() 与 deallocate()

如何制作具有 2 个视图的 3D 旋转动画?

如何使用 ISO 国家代码(2 个字母)查找区域设置标识符

Reality Composer Tap Trigger 问题

在视图中设置所有变量

理解 Swift 2.2 Select 器语法 - #selector()

如何在 SwiftUI 中打开 ImagePicker?

调整文本的字体大小以适应 UIButton

Swift中字节数组的NSData

在 Swift 中覆盖 UIScrollView 的委托属性(就像 UICollectionView 一样)

UISearchbar TextField 的高度可以修改吗?

使用 JSONEncoder 对类型为 Codable 的变量进行编码

迭代 Firebase 中的子快照