1

我有以下词典,其中包含许多子词典。如何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;
    };
}
4

3 回答 3

4

作为使用 NSPredicate 的简单替代方法,您可以使用内置的 NSDictionarykeysOfEntriesPassingTest: 这个答案假设“isChanged”是一个 NSString,值 0 或 1 是一个 NSNumber:

NSSet *theSet = [dict keysOfEntriesPassingTest:^(id key, id obj, BOOL *stop) {
    return [obj[@"isChanged"] isEqualToNumber: @1];
}];

返回的集合是通过测试的键列表。从那里,您可以删除所有匹配的内容:

[dict removeObjectsForKeys:[theSet allObjects]];

于 2012-11-24T16:24:00.363 回答
2

我通过以下方式解决了我的问题:

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];
}];
于 2012-11-29T05:26:49.820 回答
0

当您拥有字典数组时,您可以使用 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;
 }
 )
于 2012-11-24T16:04:19.470 回答