ios - 从 NSMutableDictionary 中删除多个值

标签 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 对应的对象。我当前的解决方案是修改我的删除列表以具有生成标签,然后删除,我想知道是否有其他有效的方法而不修改删除列表数组。

最佳答案

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

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

关于ios - 从 NSMutableDictionary 中删除多个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20241636/

相关文章:

ios - 如果图像的外部边界 react native ,如何剪掉图像

iPhone - 如何设置 uinavigationbar 高度?

iphone - 使用 UINavigationController 时,不会调用我的 Controller 的 viewWillAppear 或 viewDidAppear 方法

iphone - 在以下示例中释放 NSURLConnection 的正确位置是什么?

ios - NSTimeInterval 到小时和分钟的错误转换

ios - 将 UILabel 放置在倾斜或角度处

ios - 无法在 Xcode 7.1 中使用 Swift 2.0 将 UI 连接到 ViewController

javascript - Phonegap Cordova Statusbar 插件创建双条

objective-c - 使用 Interface Builder 中的 Suite 将值绑定(bind)到 NSUserDefaults

ios - 如何将这些参数传递到 NSString nsuserdefault