我正在尝试过滤来自 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_name
和 last_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]);
非常感谢任何帮助,谢谢大家
最佳答案
你想的太复杂了
stringWithFormat
在这里没有意义。- 谓词中不需要“ANY”聚合,它与 Core Data 对象中的对多关系一起使用。
[friendDictionary objectForKey:@"Data"]
返回一个数组,allObjects
这里是错误的。- 关键是“数据”,而不是“数据”。
predicateWithFormat
中的 这给了
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/