简体   繁体   English

在NSUserDefaults保存的NSMutableArray中插入对象

[英]insert object in an NSMutableArray saved with NSUserDefaults

I have an NSMutableArray saved with NSUserDefaults. 我有NSUserDefaults保存的NSMutableArray。 This array is my "favourite" items that the user can saves, so when i want to add one item, i need to read the array (from NSuserDefault) and save in the first free position. 这个数组是我用户可以保存的“最喜欢的”项目,所以当我想添加一个项目时,我需要读取数组(来自NSuserDefault)并保存在第一个空闲位置。

I'm using this method to add a value in the NSMutableArray 我正在使用此方法在NSMutableArray中添加值

-(IBAction)save{
   NSMutableArray *abc = [[NSUserDefaults standardUserDefaults] objectForKey:@"12345"];
   int n = [abc count];
   [abc insertObject:@"aaa" atIndex:n];
   NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
   [[NSUserDefaults standardUserDefaults] setObject:abc forKey:@"12345"];
   [defaults synchronize];
   [abc release];
}

what's the deal? 这是怎么回事? That if the user call this method two times, the second time the app crashes with this log: 如果用户调用此方法两次,则第二次应用程序崩溃时会显示以下日志:

* Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[__NSCFArray insertObject:atIndex:]: mutating method sent to immutable object' *由于未捕获的异常'NSInternalInconsistencyException'终止应用程序,原因:' - [__ NSCFArray insertObject:atIndex:]:发送到immutable对象的mutating方法'

why? 为什么? and why just the second time? 为什么只是第二次? The first time works fine! 第一次工作正常!

NSUserDefaults always returns immutable objects, even if the original object was mutable. 即使原始对象是可变的, NSUserDefaults始终返回不可变对象。 It's in the documentation for objectForKey : 它位于objectForKey的文档中:

The returned object is immutable, even if the value you originally set was mutable. 返回的对象是不可变的,即使您最初设置的值是可变的。

You will need to create a copy of the returned object before you modify it, using [NSMutableArray arrayWithArray:] 在修改返回对象之前,需要使用[NSMutableArray arrayWithArray:]创建返回对象的副本

Probably also best to use the arrayForKey method of NSUserDefaults if you're retrieving an array. 大概也最好使用arrayForKey的方法NSUserDefaults ,如果你检索一个数组。 Docs here: https://developer.apple.com/documentation/foundation/userdefaults 文档: https//developer.apple.com/documentation/foundation/userdefaults

Here is a complete working example: 这是一个完整的工作示例:

- (void)saveScore:(NSNumber *)score
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSMutableArray *scoreList = [[NSMutableArray alloc] initWithArray: [defaults objectForKey:@"ScoreList"]];
    [scoreList addObject:score];
    [defaults setObject:scoreList forKey:@"ScoreList"];
    [defaults synchronize];
}

First we read in the stored data in a mutable array, update the array, save the new array, and then synchronize the user defaults to permanently record the changes. 首先,我们在可变数组中读取存储的数据,更新数组,保存新数组,然后同步用户默认值以永久记录更改。

Hope this helps! 希望这可以帮助!

另一种选择是:

[[NSUserDefaults standardUserDefaults] setObject:[[[NSUserDefaults standardUserDefaults] objectForKey:@"12345"] arrayByAddingObject:object] forKey:@"12345"];

Copy Array from an NSUserDefault like this way : 像这样从NSUserDefault复制数组:

[YourArray addObjectsFromArray:[[NSUserDefaults standardUserDefaults] 
                  objectForKey:@"NSUSD"]];

or 要么

YourArray = [[NSMutableArray alloc] 
             initWithArray:[[NSUserDefaults standardUserDefaults]  
              objectForKey:@"NSUSD"]];
[YourArray addObject: (currentObject)];

快速解决:

_retrivedArray = [_mutableArray mutableCopy];

It is because you NSMutablepointer points an NSUserDefault object directly. 这是因为NSMutablepointer直接指向NSUserDefault对象。 You should first copy NSUserDefault object to an array than process it. 您应该首先将NSUserDefault对象复制到一个数组而不是处理它。

NSMutableArray *arr= [NSMutableArray arrayWithObjects:@"asd",@"dsa",nil];
    [[NSUserDefaults standardUserDefaults] setObject:arr forKey:@"12345"];
NSMutableArray *abc = [NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"12345"]];
//[[NSUserDefaults standardUserDefaults] objectForKey:@"12345"];
    int n = [abc count];
    [abc insertObject:@"aaa" atIndex:n];
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [[NSUserDefaults standardUserDefaults] setObject:abc forKey:@"12345"];
    [defaults synchronize];
    [abc release];

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

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