将NSMutableArray正确传递给NSUserDefaul

将NSMutableArray正确传递给NSUserDefaul

本文介绍了如何使用自定义数据模型将NSMutableArray正确传递给NSUserDefaults?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我先言一下,我的代码有点模糊不清。我知道名字不会好。我正在尝试使用新的iOS 8功能向我的应用添加小部件。我正在使用

Let me preface that my code has been obscured a little on purpose. I know the name won't be good. I am trying to add a widget to my app with the new iOS 8 functionality. I am using this link as a tutorial

现在,到目前为止,当我的应用程序中调用提交按钮时,我的ViewController中已经有了这个。到这时候,我将传递该数组中的所有数据。

Now, so far I have this in my ViewController when my submit button is called in my app. By this time I have all my data in that array to be passed on.

//Passes the array of times to the shared group to be called by the notification center widget.
NSUserDefaults *sharedDefaults = [[NSUserDefaults alloc] initWithSuiteName:@"group.myGroup.TodayExtensionSharingDefaults"];
NSData *encodedArray = [NSKeyedArchiver archivedDataWithRootObject:tableViewController.TimeArray];
[sharedDefaults setObject:encodedArray  forKey:@"TimesArray"];
[sharedDefaults synchronize];

但是,我的数组存储了我创建的自定义数据模型,所以在我的数据模型中我有:

However, my array stores a custom data model I created, so in my data model I have:

- (void)encodeWithCoder:(NSCoder *)encoder {
    //Encode properties, other class variables, etc
    [encoder encodeInt:_stopNumber forKey:@"stopNumber"];
    [encoder encodeObject:_route forKey:@"route"];
    [encoder encodeObject:_number forKey:@"number"];
    [encoder encodeInt:_time forKey:@"time"];
    [encoder encodeObject:_minutesOrApproaching forKey:@"@minutesOrApproaching"];
    [encoder encodeObject:_noPrediction forKey:@"noPrediction"];
}

- (id)initWithCoder:(NSCoder *)decoder {
    self = [super init];
    if(self) {
        //decode properties, other class vars
        _stopNumber = [decoder decodeIntForKey:@"stopNumber"];
        _route = [decoder decodeObjectForKey:@"route"];
        _number = [decoder decodeObjectForKey:@"number"];
        _time = [decoder decodeIntForKey:@"time"];
        _minutesOrApproaching = [decoder decodeObjectForKey:@"minutesOrApproaching"];
        _noPrediction = [decoder decodeObjectForKey:@"noPrediction"];
    }
    return self;
}

然后在我的Widget中我调用了这段代码:

And then in my Widget I have this code being called:

NSUserDefaults *defaults = [[NSUserDefaults alloc] initWithSuiteName:@"group.myGroup.TodayExtensionSharingDefaults"];
    NSData *myDecodedObject = [defaults objectForKey: @"TimesArray"];
    NSArray *decodedArray =[NSKeyedUnarchiver unarchiveObjectWithData: myDecodedObject];

    int i = 0;
    for (ETA *eta in decodedArray) {
        if(i == 0){
            _stopNumber.text = eta.number;
        }
        UILabel *label = [timeSlots objectAtIndex:i];
        label.text = eta.minutesOrApproaching;
        i++;
    }

我的问题是我一直得到:

My issue is that I keep getting:

所以我有点放弃因为我不知道是什么我在这里做错了。如果有人可以帮我指出正确的方向,那将是非常好的。再次感谢!

So I am a little off put because I don't know what I am doing wrong here. It would be really great if someone could help point me in the right direction. Thanks again!

推荐答案

因此,结果传递自定义类可能会很痛苦。虽然所有代码似乎都是正确的,但我仍然得到错误。所以我退后一步,看着我的自定义物体,问自己我真正需要的是什么?并且使用该过程决定我只需要某些值,因此我只加密那些值而不是对象。由于这些值是整数和字符串,因此它们内置于iOS中,用于编码和解码。从那里,我能够传递价值,感谢@ meda的回答和我的想法。

So, turns out passing custom classes can be a pain. And although all the code seems to be right, I am still getting the error. So I stepped back and looked at my custom object and asked myself "What is it that I really need from this?" and using that process decided I only needed certain values, so with that I encrypted only those values and not the object. Since those values were ints and strings, they are built into iOS on how to encode and decode. From there, I was able to pass the values around thanks to @meda's answer and my idea.

这篇关于如何使用自定义数据模型将NSMutableArray正确传递给NSUserDefaults?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 12:02