从NSMutableDictionary中删除多个值

时间:2013-11-27 11:47:36

标签: ios iphone objective-c nsmutablearray nsmutabledictionary

我有NSMutableDictionary *categoryTableArray;,其中包含此格式的数据

MY_CATEGORY : {
    "Generation 2" =     (
                {
            id = 34;
            name = Footwear;
        }
    );
    "Generation 3" =     (
                {
            id = 53;
            name = Party;
        }
    );
    "Generation 5" =     (
                {
            id = 72;
            name = hash1;
        },
                {
            id = 86;
            name = uhgyututyutguhgjhbj;
        }
    );
}

在选择要删除的项目时,我会在NSMutableArray *deletedArray;中获得一个id格式列表,其格式为

Delete List:(
        {
        id = 72;
    },
        {
        id = 53;
    }
)

我需要一种有效的方法来删除与categoryTableArray中删除列表中的id对应的对象。我目前的解决方案是修改我的删除列表以生成一个生成标签,然后删除,我想知道是否有任何其他有效的方法而不修改删除列表数组。

1 个答案:

答案 0 :(得分:1)

这是我为你写的一些测试代码。享受:)

- (void)filterCategories
{
    NSMutableDictionary *categoryTableArray = [@{@"Generation 2": @[@{@"id": @(34), @"name": @"Footwear"}],
                                                 @"Generation 3": @[@{@"id": @(53), @"name": @"Party"}],
                                                 @"Generation 5": @[@{@"id": @(72), @"name": @"hash1"},
                                                                    @{@"id": @(86), @"name": @"uhgyututyutguhgjhbj"}]} mutableCopy];
    NSArray *deleteList = @[@{@"id": @(72)},
                            @{@"id": @(53)}];

    NSLog(@"Before deletion:\n%@", categoryTableArray);
    [self deletIDs:deleteList fromCategories:categoryTableArray];
    NSLog(@"After deletion:\n%@", categoryTableArray);
}

- (void)deletIDs:(NSArray *)deleteList fromCategories:(NSMutableDictionary *)categories
{
    // Convert the array of dictionaries into just an array of IDs.
    NSArray *ids = [deleteList valueForKeyPath:@"id"];
    for (NSString *key in [categories allKeys])
    {
        categories[key] = [categories[key] filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"NOT id IN %@", ids]];
    }
}