xcode 搜索字典数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11731092/
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
Searching through an array of dictionaries
提问by ingenspor
I'm making an iPhone app which displays information about red wines. The wines are stored in a plist containing an array of dictionaries. I'm adding a search view controller to the application, and to start up easy I want to get the wines who the value for the "Name" key containsnameString (from textbox).
我正在制作一个 iPhone 应用程序,它显示有关红酒的信息。葡萄酒存储在包含一系列字典的 plist 中。我正在向应用程序添加一个搜索视图控制器,为了轻松启动,我想获取“Name”键的值包含nameString(来自文本框)的葡萄酒。
It kind of stops here, I need some advices about the most suitable way of doing this. Is there a function in the NSArray class which will do the job, should bring in NSPredicate, NSUserDefaults, or are there other options? I've done some research but I'm going to need some advices and maybe an example to get started.
它有点停在这里,我需要一些关于最合适的方法的建议。NSArray 类中是否有一个函数可以完成这项工作,应该引入 NSPredicate、NSUserDefaults,还是有其他选项?我已经做了一些研究,但我需要一些建议,也许需要一个例子来开始。
I will advance the search function to let the user include/exclude countries, get wines that suit this and that food, set minimum price, maximum price, and so on. The dictionaries have strings for all this info. So before I start on something advanced like this I'll need some advice for which functions could do my job the best.
我将推进搜索功能,让用户包括/排除国家/地区,获取适合这种和那种食物的葡萄酒,设置最低价格、最高价格等等。字典有所有这些信息的字符串。所以在我开始像这样的高级东西之前,我需要一些建议,哪些功能可以最好地完成我的工作。
-(IBAction)searchButtonPoke:(id)sender{
NSString *path = [[NSBundle mainBundle] pathForResource:@"Wines" ofType:@"plist"];
allObjectsArray = [[NSMutableArray alloc] initWithContentsOfFile:path];
NSString *nameString = [NSString stringWithFormat:@"%@", [nameTextField text]];
resultObjectsArray = /*objects where the value for the key "Name" contains
nameString*/;
回答by anka
How about simply iterating through the array and compare the names?
简单地遍历数组并比较名称怎么样?
resultObjectsArray = [NSMutableArray array];
for(NSDictionary *wine in allObjectsArray)
{
NSString *wineName = [wine objectForKey:@"Name"];
NSRange range = [wineName rangeOfString:nameString options:NSCaseInsensitiveSearch];
if(range.location != NSNotFound)
[resultObjectsArray addObject:wine];
}
Swift version is even simpler:
Swift 版本更简单:
let resultObjectsArray = allObjectsArray.filter{
(for (NSDictionary* dict in Array) {
if ([[dict objectForKey:@"key"] isEqualToString:string]) {
Index = [Array indexOfObject:dict];
}
}
["Name"] as! String).range(of: nameString,
options: .caseInsensitive,
range: nil,
locale: nil) != nil
}
Cheers, anka
干杯,安卡
回答by Mangesh
This Works !!! tested !!!
这有效!!!测试!!!
NSPredicate *predicate =
[NSPredicate predicateWithFormat:@"publisher == %@", @"Apress" ];
NSArray *filtered = [bookshelf filteredArrayUsingPredicate:predicate];
回答by user864279
We can use NSPredicate too, like this:
我们也可以使用 NSPredicate,像这样:
##代码##If the publisher can be found in the bookshelf array, filtered count will be bigger than 0.
如果可以在 bookshelf 数组中找到出版商,则过滤后的计数将大于 0。
I thinks this way is much cleaner way to search. Hope it helps.
我认为这种方式是更清洁的搜索方式。希望能帮助到你。
回答by r farnell
remember to break; out your for loop once your object has been found
记得打破; 找到对象后退出 for 循环