iPhone - 解析 NSMutableArray 以删除对象

发布于 2024-11-17 06:56:58 字数 1751 浏览 3 评论 0原文

我有一个作用于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

很酷不放纵 2024-11-24 06:56:58

在快速枚举数组时决不能更改数组。它会引发异常,就像为您所做的那样。您可以使用 Modified deleteSavedItem: 获取满足条件的项目

NSArray * matchingItems = [savedItems filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@" keycode MATCHES[cd] %@ ", keycodeToDelete]];

,然后使用

[savedItems removeObjectsInArray:matchingItems];

Modified deleteSavedItem: 删除它们。

- (void)deleteSavedItem:(NSString *)keycodeToDelete {
    NSMutableArray * savedItems    = [[self loadAllSavedItems] mutableCopy];
    NSArray        * matchingItems = [savedItems filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@" keycode MATCHES[cd] %@ ", keycodeToDelete]];

    [savedItems removeObjectsInArray:matchingItems];

    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
    [defaults setValue:savedItems forKey:@"savedItems"];
    [defaults synchronize];

    [savedItems release];
}

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

NSArray * matchingItems = [savedItems filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@" keycode MATCHES[cd] %@ ", keycodeToDelete]];

and then remove them using,

[savedItems removeObjectsInArray:matchingItems];

Modified deleteSavedItem: would be.

- (void)deleteSavedItem:(NSString *)keycodeToDelete {
    NSMutableArray * savedItems    = [[self loadAllSavedItems] mutableCopy];
    NSArray        * matchingItems = [savedItems filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@" keycode MATCHES[cd] %@ ", keycodeToDelete]];

    [savedItems removeObjectsInArray:matchingItems];

    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
    [defaults setValue:savedItems forKey:@"savedItems"];
    [defaults synchronize];

    [savedItems release];
}
贱人配狗天长地久 2024-11-24 06:56:58

您可以尝试同步 NSUSerDefaults 数组中的添加/删除

You could try synchonizing the add/remove from the NSUSerDefaults array

九八野马 2024-11-24 06:56:58
        // ==========================================================================================================
    - (void) deleteSavedItem:(NSString*)keycodeToDelete
        // ==========================================================================================================
    {   
        NSMutableArray* savedItems = [self loadAllSavedItems];

        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];
    }

        // ==========================================================================================================
    - (NSMutableArray*) loadAllSavedItems 
        // ==========================================================================================================
    {
        NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
        NSMutableArray* savedItems = [defaults arrayForKey:@"savedItems"];
        if (savedItems == nil) savedItems = [[NSMutableArray alloc]init];
        return savedItems;
    }
        // ==========================================================================================================
    - (void) deleteSavedItem:(NSString*)keycodeToDelete
        // ==========================================================================================================
    {   
        NSMutableArray* savedItems = [self loadAllSavedItems];

        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];
    }

        // ==========================================================================================================
    - (NSMutableArray*) loadAllSavedItems 
        // ==========================================================================================================
    {
        NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
        NSMutableArray* savedItems = [defaults arrayForKey:@"savedItems"];
        if (savedItems == nil) savedItems = [[NSMutableArray alloc]init];
        return savedItems;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文