【问题标题】:NSDictionary Predicate filter on child objectsNSDictionary Predicate 过滤子对象
【发布时间】:2012-08-19 18:38:44
【问题描述】:

我正在尝试过滤来自 Facebook 的 API 的响应,JSON 如下所示:

{ 
 "Data": [
  {  
    "first_name" : "Joe",
    "last_name" : "bloggs",
    "uuid" : "123"
  },
  {  
    "first_name" : "johnny",
    "last_name" : "appleseed",
    "uuid" : "321"
  }
 ]
}

我将它加载到一个 NSDictionary 中,像 [[friendDictionary objectForKey:@"data"] allObjects] 一样访问它

我现在尝试根据有人在文本字段中输入姓名的时间来根据first_namelast_name 进行过滤。这就是我所拥有的,但它非常失败:

  NSPredicate *filter = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"ANY.first_name beginswith[c] %@ OR ANY.last_name beginswith[c] %@", friendField.text, friendField.text]];
  NSLog(@"%@", [[[friendDictionary objectForKey:@"data"] allObjects] filteredArrayUsingPredicate:filter]);

非常感谢任何帮助,谢谢大家

【问题讨论】:

    标签: ios nsdictionary nspredicate


    【解决方案1】:

    你想的太复杂了。

    • stringWithFormat 内的 predicateWithFormat 在这里没有意义。
    • 不需要谓词中的“ANY”聚合,它与 Core Data 对象中的多对多关系一起使用。
    • [friendDictionary objectForKey:@"Data"] 返回一个数组,allObjects 这里错了。
    • 关键是“数据”,而不是“数据”。

    这给了

    NSPredicate *filter = [NSPredicate predicateWithFormat:@"first_name beginswith[c] %@ OR last_name beginswith[c] %@",
        friendField.text, friendField.text];
    NSArray *filteredArray = [[friendDictionary objectForKey:@"Data"] filteredArrayUsingPredicate:filter];
    

    【讨论】:

      猜你喜欢
      • 2012-05-17
      • 2015-01-12
      • 2013-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多