ios - 子对象上的 NSDictionary 谓词过滤器

标签 ios nsdictionary nspredicate

我正在尝试过滤来自 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]);

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

最佳答案

你想的太复杂了

    predicateWithFormat 中的
  • stringWithFormat 在这里没有意义。
  • 谓词中不需要“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];

关于ios - 子对象上的 NSDictionary 谓词过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12028976/

相关文章:

ios - 在 Swift 中为我的函数实现更通用的方法

ios - 只添加当前不存在的对象到 Realm 数据库

iphone - 在 iOS 中检测拖动时的碰撞

ios - 如何获取有关月份和年份的数据

ios - 按字母顺序对 plist 中的项目进行排序

cocoa - 在 NSTableView 中显示图像

ios - 如何将RadioButtons放入表单?

ios - 是否可以在 iOS 后台使用 Google Nearby Messages 发布消息?

ios - 按 int 值过滤 NSArray 和 NSDictionary

ios - 使用 Coredata 或 NSMutableArray 的 NSPredicate 进行过滤的最佳方法?