我有有许多子词典的词典folloving。 我怎样才能在那里删除对象isChanged = 1
使用从母体字典NSPredicate
?
{
"0_496447097042228" = {
cellHeight = 437;
isChanged = 1;
};
"100000019882803_193629104095337" = {
cellHeight = 145;
isChanged = 0;
};
"100002140902243_561833243831980" = {
cellHeight = 114;
isChanged = 1;
};
"100004324964792_129813607172804" = {
cellHeight = 112;
isChanged = 0;
};
"100004324964792_129818217172343" = {
cellHeight = 127;
isChanged = 0;
};
"100004324964792_129835247170640" = {
cellHeight = 127;
isChanged = 1;
};
}
作为一个简单的替代使用NSPredicate,您可以使用的NSDictionary的内置keysOfEntriesPassingTest:
这个答案假定“isChanged”是一个NSString和值为0或1是一个NSNumber:
NSSet *theSet = [dict keysOfEntriesPassingTest:^(id key, id obj, BOOL *stop) {
return [obj[@"isChanged"] isEqualToNumber: @1];
}];
返回的集合是通过测试键的列表。 从那里,你可以删除所有与匹配:
[dict removeObjectsForKeys:[theSet allObjects]];
我解决我的问题通过以下方式:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"isChanged == %d", 1];
NSArray *allObjs = [parentDict.allValues filteredArrayUsingPredicate:predicate];
[allObjs enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSMutableArray *keys = [[NSMutableArray alloc] initWithCapacity:0];
[keys setArray:[parentDict allKeysForObject:obj]];
[parentDict removeObjectsForKeys:keys];
[keys release];
}];
当你有字典的数组比你可以使用NSPredicate删除所选类别的数据
这里是代码
NSString *selectedCategory = @"1";
//filter array by category using predicate
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"isChanged == %@", selectedCategory];
NSArray *filteredArray = [yourAry filteredArrayUsingPredicate:predicate];
[yourAry removeObject:[filteredArray objectAtIndex:0]];
但是,在你的问题的数据是不是在阵列它在字典
你的数据应该是这种格式
(
{
cellHeight = 437;
isChanged = 1;
},
{
cellHeight = 145;
isChanged = 0;
},
{
cellHeight = 114;
isChanged = 1;
}
)