iPhone - 解析 NSMutableArray 以删除对象
我有一个作用于 UserDefaults 的对象。 它将事物添加到用户默认值的数组中,然后在发生某些事件时将其删除。 该对象可以有多个实例。
来自不同对象实例的这些调用似乎会发生冲突并产生崩溃:
*** Terminating app due to uncaught exception 'NSGenericException', reason: '*** Collection <__NSCFArray: 0x66061e0> was mutated while being enumerated.<CFArray 0x66061e0 [0xe38400]>{type = mutable-small, count = 3, values = (
我像这样删除我的项目:
// ==========================================================================================================
- (void) deleteSavedItem:(NSString*)keycodeToDelete
// ==========================================================================================================
{
NSMutableArray* savedItems = [[self loadAllSavedItems] mutableCopy];
for (NSDictionary* dict in savedItems) {
NSString* keycode = [dict objectForKey:@"keycode"];
if ([keycode isEqualToString:keycodeToDelete])
[savedItems removeObject:dict];
}
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
[defaults setValue:savedItems forKey:@"savedItems"];
[defaults synchronize];
[savedItems release];
}
// ==========================================================================================================
- (NSArray*) loadAllSavedItems
// ==========================================================================================================
{
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
NSArray* savedItems = [defaults arrayForKey:@"savedItems"];
if (savedItems == nil) savedItems = [NSArray array];
return savedItems;
}
如何在不发生冲突的情况下从该数组添加/删除内容?
我不确定原因是许多使用默认值的对象导致了这次崩溃。我也怀疑这就是我解析数组的方式。
我需要保留我的对象实例,并让它们并行运行进程。
I have an object that act on UserDefaults.
It adds things into an Array into userdefaults, then removes it when some events occurs.
That object can have more than one instance.
Those calls coming from different objects instance seems to collide and generate a crash :
*** Terminating app due to uncaught exception 'NSGenericException', reason: '*** Collection <__NSCFArray: 0x66061e0> was mutated while being enumerated.<CFArray 0x66061e0 [0xe38400]>{type = mutable-small, count = 3, values = (
I remove my items like this :
// ==========================================================================================================
- (void) deleteSavedItem:(NSString*)keycodeToDelete
// ==========================================================================================================
{
NSMutableArray* savedItems = [[self loadAllSavedItems] mutableCopy];
for (NSDictionary* dict in savedItems) {
NSString* keycode = [dict objectForKey:@"keycode"];
if ([keycode isEqualToString:keycodeToDelete])
[savedItems removeObject:dict];
}
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
[defaults setValue:savedItems forKey:@"savedItems"];
[defaults synchronize];
[savedItems release];
}
// ==========================================================================================================
- (NSArray*) loadAllSavedItems
// ==========================================================================================================
{
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
NSArray* savedItems = [defaults arrayForKey:@"savedItems"];
if (savedItems == nil) savedItems = [NSArray array];
return savedItems;
}
How may I add/remove things from that array without collision ?
I'm not sure the reason is the many objects that use default that causes this crash. I suspect too that this is the way I parse the array.
I need to keep my objects instances, and keep them running process parralel.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在快速枚举数组时决不能更改数组。它会引发异常,就像为您所做的那样。您可以使用 Modified
deleteSavedItem:
获取满足条件的项目,然后使用
Modified
deleteSavedItem:
删除它们。You must never alter an array while fast enumerating it. It will raise an exception as it did for you. You can get the items that satisfy your condition using
and then remove them using,
Modified
deleteSavedItem:
would be.您可以尝试同步 NSUSerDefaults 数组中的添加/删除
You could try synchonizing the add/remove from the NSUSerDefaults array