使用 NSKeyedArchiver 将对象传递给 WatchKit

Passing Objects To WatchKit with NSKeyedArchiver

class Parent: NSObject, NSCoding {
    var name : String?
    var childs : [Child]?

    override init() {}

    func encode(with aCoder: NSCoder) {
        aCoder.encode(name, forKey: "name")
        aCoder.encode(childs, forKey: "childs")
    }

    required init(coder aDecoder: NSCoder) {
        name = aDecoder.decodeObject(forKey: "name") as? String
        childs = aDecoder.decodeObject(forKey: "childs") as? [Child]

        super.init()
    }
}

class Child: NSObject, NSCoding {
    var name: String?

    override init() {}

    func encode(with aCoder: NSCoder) {
        aCoder.encode(name, forKey: "name")
    }

    required init(coder aDecoder: NSCoder) {
        name = aDecoder.decodeObject(forKey: "name") as? String
        super.init()
    }
}

// iOS side:

public func session(_ session: WCSession, didReceiveMessage message: [String : Any], replyHandler: @escaping ([String : Any]) -> Swift.Void) {

    var parent = Parent()
    parent.name = "John"

    var child1 = Child()
    var child2 = Child()

    parent.childs = [child1, child2]

    NSKeyedArchiver.setClassName("Parent", for: Parent.self)

    let data = NSKeyedArchiver.archivedData(withRootObject: parent)
    replyHandler(["parent": data as AnyObject])

}

// watchOS side:

if WCSession.isSupported() {

    session = WCSession.default()
    session!.sendMessage(["getParent": "getParent"], replyHandler: { (response) -> Void in

        if let rawParent = response as? [String:Data] {

            NSKeyedUnarchiver.setClass(Parent.self, forClassName: "Parent")
            if let parent = NSKeyedUnarchiver.unarchiveObject(with: data) as? Parent {
                // do something
            }

        }
    }, errorHandler: { (error) -> Void in
        print(error)
    })
}

在这种情况下,如您所见,我使用了有效的 NSKeyedUnarchiver.setClass 和 NSKeyedArchiver.setClassName 方法,但我仍然遇到运行时崩溃: [NSKeyedUnarchiver decodeObjectForKey:]: 无法为键 (NS.objects)

解码 class (Child) 的对象

如果删除 NSKeyedUnarchiver.setClass(Parent.self, forClassName: "Parent")NSKeyedArchiver.setClassName("Parent", for: Parent.self) 行,我会得到相同的错误,但对于父级 class。所以我确定我必须对 Child 使用相同的方法调用,但我不知道在哪里。 有人知道吗?

好吧,我找到了解决方案,如果你有相同的结构,你只需简单地设置 class 个名字

NSKeyedArchiver.setClassName("Parent", for: Parent.self)
NSKeyedArchiver.setClassName("Child", for: Child.self)

解压时也是如此:

NSKeyedUnarchiver.setClass(Parent.self, forClassName: "Parent")
NSKeyedUnarchiver.setClass(Child.self, forClassName: "Child")