NSDictionary对子对象进行谓词过滤

Dex*_*url 0 nsdictionary nspredicate ios

我正在尝试过滤来自Facebook API的响应,JSON看起来像这样:

{ 
 "Data": [
  {  
    "first_name" : "Joe",
    "last_name" : "bloggs",
    "uuid" : "123"
  },
  {  
    "first_name" : "johnny",
    "last_name" : "appleseed",
    "uuid" : "321"
  }
 ]
}
Run Code Online (Sandbox Code Playgroud)

我将它加载到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]);
Run Code Online (Sandbox Code Playgroud)

非常感谢任何帮助,谢谢你们

Mar*_*n R 5

你觉得太复杂了.

  • stringWithFormatpredicateWithFormat这里毫无意义.
  • 不需要谓词中的"ANY"聚合,它与Core Data对象中的to-many-relationships一起使用.
  • [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];
Run Code Online (Sandbox Code Playgroud)