WatchKit NSUserDefaults and NSKeyedUnarchiver issue

孤街浪徒 提交于 2019-12-01 11:21:50

问题


In my project I have a custom object called Country, which implements NSCoding

The code is shared across iphone and watch app by reference. Both app are in the same app group.

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")
    }
}

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)'

回答1:


Had the same problem, this helped me: https://devforums.apple.com/message/1093079#1093079



来源:https://stackoverflow.com/questions/28383549/watchkit-nsuserdefaults-and-nskeyedunarchiver-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!