NSInvalidUnarchiveOperationException无法解码Apple Watch扩展中的对象错误 [英] NSInvalidUnarchiveOperationException cannot decode object error in Apple Watch extension

查看:106
本文介绍了NSInvalidUnarchiveOperationException无法解码Apple Watch扩展中的对象错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用户对象,需要将其存储在NSUserDefaults中并与iOS 8扩展应用程序(Watchkit)共享.在主容器应用中,我可以毫无问题地对对象进行编码和解码.但是,当我尝试在扩展中检索存储的用户对象时,出现了"'NSInvalidUnarchiveOperationException', reason: '*** -[NSKeyedUnarchiver decodeObjectForKey:]: cannot decode object of class"错误.

I've got a user object that I need to store in NSUserDefaults and share with an iOS 8 extension app (Watchkit). In the main container app, I can encode and decode the object without any problem. However, when I try to retrieve the stored user object in the Extension, I get a "'NSInvalidUnarchiveOperationException', reason: '*** -[NSKeyedUnarchiver decodeObjectForKey:]: cannot decode object of class" error.

据我所知,NSCoding已在对象中正确实现(并且我能够在主"应用中对对象进行编码和解码).

As far as I can see NSCoding has been implemented correctly in the object (and I'm able to encode and decode the object in the 'main' app).

容器"应用中的代码,用于存储用户对象.

Code in 'Container' app to store user object.

//Store user data in NSUserDefaults
NSUserDefaults *defaults = [[NSUserDefaults alloc] initWithSuiteName:@"group.com.mygroup"];

SFUserAccount *user = [SFUserAccountManager sharedInstance].currentUser;

NSData *userEncodedObject = [NSKeyedArchiver archivedDataWithRootObject:user];
[defaults removeObjectForKey:@"SF User Acct1"]; //remove any old values
[defaults setObject:userEncodedObject forKey:@"SF User Acct1"];

[defaults synchronize];
SFUserAccount *decodedUser =  [NSKeyedUnarchiver unarchiveObjectWithData:userEncodedObject];

上面在主应用程序中执行测试解码的最后一行工作正常.

The last line above to perform a test decode in the main app works fine.

从NSUserDefaults检索并在扩展目标中解码的代码如下.

The code to retrieve from NSUserDefaults and decode in the extension target is below.

NSUserDefaults *defaults = [[NSUserDefaults alloc] initWithSuiteName:@"group.com.mygroup"];
    NSData *archivedUser = [defaults objectForKey:@"SF User Acct1"];



    if (archivedUser){
        SFUserAccount *user =  [NSKeyedUnarchiver unarchiveObjectWithData:archivedUser];
    }

在扩展代码中,我得到一个'NSInvalidUnarchiveOperationException',原因:'***-[NSKeyedUnarchiver encodeObjectForKey:]:无法解码类的对象"

In the extension code, I get a "'NSInvalidUnarchiveOperationException', reason: '*** -[NSKeyedUnarchiver decodeObjectForKey:]: cannot decode object of class"

关于我应该从哪里开始寻找的任何建议?该应用程序编译良好,这使我相信扩展目标中已包含必需的框架.

Any suggestions as to where I should start looking? The app compiles fine which leads me to believe that the required frameworks have been included in the Extension target.

推荐答案

要使您的WatchKit扩展能够解码SFUserAccount对象,它需要了解SFUserAccount类.要启用此功能,应将其添加到您的WatchKit扩展中.

For your WatchKit Extension to be able to decode SFUserAccount objects, it needs to understand the SFUserAccount class. To enable this, it should be added to your WatchKit Extension.

  1. 在项目浏览器"中的Xcode左列顶部,单击您的项目名称. (按Cmd-1以显示项目浏览器"是否已隐藏.
  2. 单击以在主窗口左侧的目标"下突出显示WatchKit Extension的名称. (请注意:突出显示您的WatchKit 扩展,而不是WatchKit应用.
  3. 在顶部的项目中选择构建阶段".
  4. 在编译源代码"旁边,使用显示三角形查看该部分(如果该部分尚不可见).
  5. 点击该部分底部的"+"以添加新的来源.
  6. 从列表中,为您要添加的类选择.m文件(在本例中为SFUserAccount.m文件).
  7. 点击添加".
  8. 构建并运行.
  1. Click on your project name in the Project Navigator, at the top of the left column in Xcode. (Press Cmd-1 to show the Project Navigator if it is hidden.
  2. Click to highlight the name of your WatchKit Extension on the left of the main window, under 'Targets'. (NOTE: highlight your WatchKit Extension, not the WatchKit App.
  3. Select 'Build Phases' in the items across the top.
  4. Next to 'Compile Sources', use the disclosure triangle to view the section if it is not already visible.
  5. Click the '+' at the bottom of the section to add a new source.
  6. From the list, select the .m file for the Class you need to add (in this case, the SFUserAccount.m file).
  7. Click 'Add'.
  8. Build and run.

这篇关于NSInvalidUnarchiveOperationException无法解码Apple Watch扩展中的对象错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆