xcode 字典数组的 NSPredicate
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21201251/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
NSPredicate for Array of Dictionaries
提问by anotherDeveloper
I have an
我有一个
Array
Item 0 -- Dictionary
Sport -- String
Mens -- Array
Item 0 -- Dictionary
Name -- String
Rules -- String
Description -- String
Womens -- Array
Item 0 -- Dictionary
Name -- String
Rules -- String
Description -- String
Item 1 -- Dictionary
And so on.....
I would like to create a NSPredicate searching if a given string is contained inside of Name. How can I achieve reaching that deep?
如果给定的字符串包含在 Name 中,我想创建一个 NSPredicate 搜索。我怎样才能达到那么深?
If you cant understand the graph. I have an array full of dictionaries, Inside the dictionaries are A string, An Array of dictionaries, and An Array of dictionaries, Inside of the dictionaries are string objects
如果你看不懂图表。我有一个充满字典的数组,字典里面是一个字符串、一个字典数组和一个字典数组,字典里面是字符串对象
So How do I get into the second dictionaries and search the Name key
那么如何进入第二个词典并搜索 Name 键
Thanks in advance
提前致谢
回答by malex
You need the following
您需要以下内容
NSString *str = <search string>;
NSPredicate *pred = [NSPredicate predicateWithFormat:@"ANY Mens.Name LIKE %@ OR ANY Womens.Name LIKE %@", str, str];
NSArray *result = [your_array filteredArrayUsingPredicate:pred];
BOOL success = result.count > 0;
回答by Rohan Bhale
To get to the name you have to do this:
要获得名称,您必须执行以下操作:
for (NSDictionary *itemDict in myArray)
{
NSArray *womensArray = (NSArray*)[itemDict objectForKey:@"Womens"];
for (NSDictionary *womenItemDict in womensArray)
{
NSString *name = [womenItemDict objectForKey:@"Name"];
//Do the comparison here
}
}