我有一个 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/