WatchKit NSUserDefaults 和 NSKeyedUnarchiver 问题

WatchKit NSUserDefaults and NSKeyedUnarchiver issue

在我的项目中,我有一个名为 Country 的自定义对象,它实现了 NSCoding

代码在 iphone 和 watch 应用之间共享,供参考。这两个应用程序在同一个应用程序组中。

在我的 iPhone 应用程序中,我使用 NSUserDefaults 来描述一个 Country 对象,我正在使用以下代码从 watch 应用程序读取相同的对象:

var defaults = NSUserDefaults(suiteName: "group.my.group")
if let data = defaults?.objectForKey("country") as? NSData {
    if let country = NSKeyedUnarchiver.unarchiveObjectWithData(data) as? Country {
        println("done")
    }
}

此代码在 iPhone 应用程序中运行完美,国家变量已正确创建,在 Watch 应用程序中它抛出错误:

Terminating app due to uncaught exception 'NSInvalidUnarchiveOperationException', reason: '*** -[NSKeyedUnarchiver decodeObjectForKey:]: cannot decode object of class (Country)'

有同样的问题,这对我有帮助:https://devforums.apple.com/message/1093079#1093079