【问题标题】:Remove objects using NSPredicate使用 NSPredicate 删除对象
【发布时间】:2012-11-24 12:51:01
【问题描述】:

我有以下词典,其中包含许多子词典。 如何使用NSPredicate从父字典中删除isChanged = 1的对象?

{
    "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;
    };
}

【问题讨论】:

  • 你的问题是怎么回事..?
  • 好的兄弟,然后接受他的回答和+1,如果你有这种格式的数据,我的代码也可以工作
  • @Rajneesh071 下面我添加了我的解决方案作为答案
  • 干得好......你的答案是两个答案的结合......很好......

标签: iphone objective-c ios nsdictionary nspredicate


【解决方案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]];

【讨论】:

  • 块中的代码可以简化为: return [obj[@"isChanged"] isEqual: @1];不需要 if-else 子句。
  • 您不需要避免使用 iOSdeveloper.apple.com/library/ios/#releasenotes/ObjectiveC/… 向后兼容 iOS4,并且您使用的基于块的过滤方法可从 iOS4 获得以后 - 所以不需要编写两个方法。
  • 为了清楚起见,我打算留下冗长的答案,但我认为你是对的@rdelmar,即使没有子句也很清楚。
  • @JShapiro 最后 - 很抱歉很挑剔 - 但是应该用isEqualToNumber: 进行比较这不应该被依赖。
  • @Abizern 感谢您的挑剔:) 编辑答案。
【解决方案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];
}];

【讨论】:

    【解决方案3】:

    当您拥有字典数组时,您可以使用 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;
     }
     )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-02
      • 2012-05-17
      • 1970-01-01
      相关资源
      最近更新 更多