Swift-XPC的自定义类未加载

时间:2020-03-20 18:45:10

标签: swift xpc

我有一个如下定义的Person类,我试图通过XPC连接将其传递给方法。我的课如下:

@objc public class Person: NSObject, NSSecureCoding {

    public static var supportsSecureCoding: Bool {
        return true
    }

    public var firstName: String
    public var lastName: String
    public var age: Int32
    public var weight: Int32

    public required init?(coder: NSCoder) {
        self.firstName = coder.decodeObject(of: [NSString.self], forKey: "firstNameKey") as! String
        self.lastName = coder.decodeObject(of: [NSString.self], forKey: "lastNameKey") as! String
        self.age = coder.decodeInt32(forKey: "ageKey")
        self.weight = coder.decodeInt32(forKey: "weightKey")
    }

    public init(firstName: String, lastName: String, age: Int32, weight: Int32) {
        self.firstName = firstName
        self.lastName = lastName
        self.age = age
        self.weight = weight
    }

    public func encode(with coder: NSCoder) {
        coder.encode(firstName, forKey: "firstNameKey")
        coder.encode(lastName, forKey: "lastNameKey")
        coder.encode(age, forKey: "ageKey")
        coder.encode(weight, forKey: "weightKey")
    }
}

我正在使用的协议如下:

@objc public protocol XPCProtocol {
    @objc func handlePerson(_ person: Person)
}

我尝试如下调用handlePerson

let person = Person(firstName: "John", lastName: "Doe", age: 45, weight: 180)
proxy.handlePerson(person)

但是当我尝试调用Person时,我的handlePerson类未加载错误:

2020-03-20 12:37:31.840163-0600 TestApp[3451:66436] [xpc.exceptions] <NSXPCConnection: 0x600003010000> connection to service on pid 3457 named ABCDEFGH.com.my.test.bundle.xpc: Exception caught during decoding of received selector handlePerson:, dropping incoming message.
Exception: Exception while decoding argument 0 (#2 of invocation):
Exception: decodeObjectForKey: class "MyModule.Person" not loaded or does not exist

This personthis person似乎也遇到了类似的问题,可以通过将类签名更改为@objc(Person) public class Person来解决,但在我的情况下,我会收到相同的错误消息,但不能找到Person而不是MyModule.Person

如何解决此问题,以便可以通过XPC传递自定义对象?

0 个答案:

没有答案