WatchKit NSUserDefaults和NSKeyedUnarchiver问题 [英] WatchKit NSUserDefaults and NSKeyedUnarchiver issue
问题描述
在我的项目中,我有一个名为Country的自定义对象,该对象实现了NSCoding
In my project I have a custom object called Country, which implements NSCoding
该代码通过引用在iPhone和Watch App之间共享.两个应用程序都在同一个应用程序组中.
The code is shared across iphone and watch app by reference. Both app are in the same app group.
在我的iPhone应用程序中,我正在使用NSUserDefaults讲述一个Country对象,而在watch应用程序中,我正在使用以下代码读取同一对象:
In my iPhone app I'm using NSUserDefaults to story a Country object, from watch app I'm reading the same object with following code:
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应用程序中它抛出了错误:
This code in iPhone app works perfectly and the country variable is correctly created, in the Watch app it is throwing an error:
Terminating app due to uncaught exception 'NSInvalidUnarchiveOperationException', reason: '*** -[NSKeyedUnarchiver decodeObjectForKey:]: cannot decode object of class (Country)'
推荐答案
Had the same problem, this helped me: https://devforums.apple.com/message/1093079#1093079
这篇关于WatchKit NSUserDefaults和NSKeyedUnarchiver问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!