简体   繁体   English

使用NSUserDefaults与Today Extension(小部件)共享一组自定义对象

[英]Sharing an array of custom objects with Today Extension (widget) with NSUserDefaults

this is my first stack post so please be constructive when reviewing my posting technique! 这是我的第一篇论文帖子,所以在审核我的帖子技巧时请建设性的!

Basically, my problem is that I have an array of custom objects that I need to share with a today extension. 基本上,我的问题是我有一系列自定义对象,我需要与今天的扩展共享。 The objects represent tasks in a to-do list, and their properties are used to store info about each task (name, location, dueDate, thumbnail, etc). 对象表示待办事项列表中的任务,其属性用于存储有关每个任务的信息(名称,位置,dueDate,缩略图等)。 The objects are stored in an array which is used to populate my to-do list. 对象存储在一个数组中,该数组用于填充我的待办事项列表。 All I want to do is pass this array to my widget so that I can populate a second tableview which will act as a condensed version of the first (for the widget view). 我想要做的就是将这个数组传递给我的小部件,这样我就可以填充第二个tableview,它将作为第一个的压缩版本(对于小部件视图)。

I should point out that my widget is properly set up, as in I have properly linked it and the containing app together in 'groups'. 我应该指出我的小部件已正确设置,因为我已将它与包含的应用程序正确地链接在“组”中。 I have also successfully used NSUserDefaults to pass an array of NSStrings to the widget, however, when I try to pass the array of objects to the widget, it crashes and my log reads: 我还成功地使用NSUserDefaults将NSStrings数组传递给窗口小部件,但是,当我尝试将对象数组传递给窗口小部件时,它崩溃了,我的日志显示为:

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

I understand that this crash is related to archiving the object (Xitem), which seems to be a necessary step towards saving custom objects in NSUserDefaults. 我知道这次崩溃与归档对象(Xitem)有关,这似乎是在NSUserDefaults中保存自定义对象的必要步骤。 However, I have tested saving/loading the array within the same class of the containing app, and that works fine! 但是,我已经测试了在包含应用程序的同一类中保存/加载数组,并且工作正常! (code below) (以下代码)

        NSData *encodedObject = [NSKeyedArchiver archivedDataWithRootObject:self.Xitems];
        NSUserDefaults *defaults = [[NSUserDefaults alloc] initWithSuiteName:@"group.AaronTest"];
        [defaults setObject:encodedObject forKey:@"myArray"];
        [defaults synchronize];


        NSUserDefaults *defaults2 = [[NSUserDefaults alloc] initWithSuiteName:@"group.AaronTest"];
        NSData *encodedObject2 = [defaults2 objectForKey:@"myArray"];
        NSArray *array2 = [NSKeyedUnarchiver unarchiveObjectWithData:encodedObject2];

        for (Xitem *t in array2){

            NSLog(@"*****%@*****", t.itemName);
        }

Okay so as explained, the above code works as expected. 好的,正如所解释的,上面的代码按预期工作。 However, when i insert the second 'unarchiver' half of this code into my today widget, i get the aforementioned error. 但是,当我将此代码的第二个“unarchiver”插入我今天的小部件时,我得到上述错误。 Below is my code to show how I encode/decode the object (it may be worth noting that this object was created for the simplicity of my debugging and only contains a NSString property): 下面是我的代码,以显示我如何编码/解码对象(可能值得注意的是,此对象是为了简化我的调试而创建的,只包含NSString属性):

Xitem.h Xitem.h

#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>


@interface Xitem : NSObject <NSCoding> 
{ 
NSString *itemName;
}

-(void)encodeWithCoder:(NSCoder*)encoder;
-(id)initWithCoder:(NSCoder*)decoder;

@property NSString *itemName;

@end

Xitem.m Xitem.m

#import "Xitem.h"

@implementation Xitem

@synthesize itemName;

-(void)encodeWithCoder:(NSCoder*)encoder
{

[encoder encodeObject:self.itemName forKey:@"iName"];

}

-(id)initWithCoder:(NSCoder*)decoder
{

self = [super init];

self.itemName = [decoder decodeObjectForKey:@"iName"];

return self;
}

@end

I could also post my widget and containing app code, but it doesn't differ from the first set of code i posted (apart from the renamed variables such as 'defaults2'). 我也可以发布我的小部件并包含应用程序代码,但它与我发布的第一组代码没有区别(除了重命名的变量,例如'defaults2')。 I should point out that I really have exhausted resources while trying to solve this problem, but the fact that using NSKeyedArchiver solely in the containing app works, has left me stumped. 我应该指出,在尝试解决这个问题时我确实耗尽了资源,但是仅仅在包含应用程序中使用NSKeyedArchiver这一事实让我感到难过。

I realise that this post is very similar to my own problem, but the author decides to opt for a workaround, whereas I would actually like to know why this doesn't work. 我意识到这篇文章与我自己的问题非常相似,但作者决定选择一种解决方法,而我实际上想知道为什么这不起作用。 I'm a new developer and I'm doing my best to pickup on the best working practices so any advice would be greatly appreciated. 我是一名新开发人员,我正在尽最大努力接受最佳工作实践,因此我们将非常感谢您的建议。

I think it's also possible to replace my object (class) with an NSDictionary? 我认为也可以用NSDictionary替换我的对象(类)? However I would like to avoid this if possible because it would cause many conflicts in the main app, but obviously if that is the correct method I will tackle that problem. 但是我想尽可能避免这种情况,因为它会在主应用程序中引起许多冲突,但显然如果这是正确的方法,我将解决这个问题。 On a side note, if a dictionary would be better than an object for my requirements (to-do list with properties of UIImage, CLLocation, etc) for any other reasons (memory or accessibility for example) please do elaborate and help me to understand why! 另外,如果一个字典比我的要求的对象(带有UIImage,CLLocation等属性的待办事项列表)出于任何其他原因(例如内存或可访问性),请做详细说明并帮助我理解为什么!

Many thanks for anyones time :) 非常感谢任何人的时间:)

Okay so I just fixed this. 好的,我刚刚解决了这个问题。 Incase anyone has the same problem, go to: 'Targets' > 'Widget' > 'Build Phases' > 'Compile Sources' > add custom class there (Xitem.m) 如果有人遇到同样的问题,请转到:'目标'>'小工具'>'构建阶段'>'编译源'>在那里添加自定义类(Xitem.m)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM