NSDictionary对子对象进行谓词过滤 [英] NSDictionary Predicate filter on child objects
本文介绍了NSDictionary对子对象进行谓词过滤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试过滤来自Facebook API的回复,JSON看起来像这样:
{
数据:[
{
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以[c]%@ OR ANY.last_name开头,以[c]%@开头,friendField.text,friendField.text]];
NSLog(@%@,[[friendDictionary objectForKey:@data] allObjects] filteredArrayUsingPredicate:filter]);
非常感谢任何帮助,谢谢你们
-
stringWithFormat
在predicateWithFormat
中没有任何意义。 - 谓词中不需要ANY聚合,即使用与Core Data对象中的多对多关系。
-
[friendDictionary objectForKey:@Data]
返回一个数组,allObjects
这里错了。 - 关键是数据,而不是数据。
这给出了
NSPredicate * filter = [NSPredicate predicateWithFormat:@first_name startswith [c]%@ OR last_name以[c]%@开头,
friendField.text,friendField.text];
NSArray * filteredArray = [[friendDictionary objectForKey:@Data] filteredArrayUsingPredicate:filter];
I'm trying to filter a response from Facebook's API, the JSON looks like this:
{
"Data": [
{
"first_name" : "Joe",
"last_name" : "bloggs",
"uuid" : "123"
},
{
"first_name" : "johnny",
"last_name" : "appleseed",
"uuid" : "321"
}
]
}
I load this into a NSDictionary, accessing it like [[friendDictionary objectForKey:@"data"] allObjects]
I'm now trying to filter based on the first_name
and last_name
based on when someone inputs a name into a textfield. Here's what I have but its failing horribly:
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]);
Any help is greatly appreciated, thanks guys
解决方案
You are thinking too complicated.
stringWithFormat
withinpredicateWithFormat
makes no sense here.- No need for the "ANY" aggregate in the predicate, that is used with to-many-relationships in Core Data objects.
[friendDictionary objectForKey:@"Data"]
returns an array,allObjects
is wrong here.- The key is "Data", not "data".
That gives
NSPredicate *filter = [NSPredicate predicateWithFormat:@"first_name beginswith[c] %@ OR last_name beginswith[c] %@",
friendField.text, friendField.text];
NSArray *filteredArray = [[friendDictionary objectForKey:@"Data"] filteredArrayUsingPredicate:filter];
这篇关于NSDictionary对子对象进行谓词过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文