简体   繁体   English

保存并加载NSMutableArray-NSUserDefaults

[英]Save and load NSMutableArray - NSUserDefaults

I am trying to save a NSMutableArray to NSUserDefaults then reload it and use it to populate the button labels. 我试图将NSMutableArray保存到NSUserDefaults然后重新加载它并使用它来填充按钮标签。 Can someone please take a look and tell me what i am doing wrong here? 有人可以看看我告诉我我在做什么错吗?

When I am loading the file to new array it appears empty. 当我将文件加载到新数组时,它显示为空。 All of the buttons I am trying to set the titles to are in ibCollectionOutlet called buttons 我试图将标题设置为的所有按钮都在ibCollectionOutlet中,称为按钮

-(void)save {
    [[NSUserDefaults standardUserDefaults] setObject:self.pressCountArray forKey:@"savedFile"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

-(void)load{

    NSMutableArray *countArray = [[[NSUserDefaults standardUserDefaults] arrayForKey:@"savedFile"] mutableCopy];

    for (NSInteger i = 0; i < [self.pressCountArray count]; i++){
        self.pressCountArray[i] = countArray[i];
    }

    for (NSInteger i = 0; i < [self.buttons count]; i++){
        UIButton *btn = self.buttons[i];
        int curCnt = [[self.pressCountArray objectAtIndex:i] integerValue];

        [btn setTitle:[NSString stringWithFormat:@"%i",curCnt] forState:UIControlStateNormal];
    }
}

I think your array contains custom objects. 我认为您的数组包含自定义对象。 If that is the case then you should implement NSCoding protocol (for serialization and de-serialization) in your custom model class. 如果是这种情况,则应在自定义模型类中实现NSCoding协议(用于序列化和反序列化)。

Implement the following NSCoding protocol methods in your class: 在您的类中实现以下NSCoding协议方法:

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

After that save the data like: 之后,将数据保存为:

NSData *encodedObject = [NSKeyedArchiver archivedDataWithRootObject:self.pressCountArray];
[[NSUserDefaults standardUserDefaults] setObject:encodedObject forKey:[NSString stringWithFormat:@"savedFile"]];

And retrieve the data like: 并像这样检索数据:

NSData *encodedObject = [[NSUserDefaults standardUserDefaults] objectForKey:[NSString stringWithFormat:@"savedFile"]];
self.pressCountArray  = [NSKeyedUnarchiver unarchiveObjectWithData:encodedObject];

It looks like you are not allocating self.pressCountArray during the load and are probably attempting to populate an empty array (in a fairly strange way). 看起来您没有在加载过程中分配self.pressCountArray ,并且可能正在尝试填充一个空数组(以一种非常奇怪的方式)。 Instead simply do: 而是简单地做:

-(void)load{

    self.pressCountArray = [[[NSUserDefaults standardUserDefaults] arrayForKey:@"savedFile"] mutableCopy];

    NSAssert([self.pressCountArray count] == [self.buttons count], @"Array count mismatch");

    for (NSInteger i = 0; i < [self.buttons count]; i++){
        UIButton *btn = self.buttons[i];
        int curCnt = [[self.pressCountArray objectAtIndex:i] integerValue];

        [btn setTitle:[NSString stringWithFormat:@"%i",curCnt] forState:UIControlStateNormal];
    }
}

Note that you need to check that the correct number of array elements have been loaded. 请注意,您需要检查是否已加载正确数量的数组元素。 I've used an NSAssert in the above code, but you probably need to return NO given it's probably something that can happen in production. 我在上面的代码中使用了NSAssert ,但是您可能需要返回NO因为这可能会在生产中发生。

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

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