从Plist搜索

时间:2013-12-19 08:39:19

标签: ios plist

有些人可以帮助我吗

我在应用启动应用时加载了plist并且我有搜索输入。当我将文本插入输入时,我想从plist

进行搜索
self.plistFile = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"xutbe"] ofType:@"plist"];
self.allData = [NSArray arrayWithContentsOfFile:self.plistFile];

我试过这样但不行的

NSString *INPUT = @"s";
self.plistFile = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"filename"] ofType:@"plist"];
self.allData = [NSArray arrayWithContentsOfFile:self.plistFile];

NSArray *searchRes = [self.allData filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"self == %@", INPUT]];
NSLog(@"%@", searchRes);

并尝试了这个而不是工作

NSString* plistPath = [[NSBundle mainBundle] pathForResource:@"xutbe" ofType:@"plist"]; NSDictionary* dict = [NSDictionary dictionaryWithContentsOfFile:plistPath]; NSDictionary* vcDict = [dict valueForKey:@"sd"]; NSLog(@"%@", vcDict);

2 个答案:

答案 0 :(得分:3)

如果您想从NSArray搜索对象,请使用 NSPredicate

NSArray *searchRes = [self.allData filteredArrayUsingPredicate:[NSPredicate
   predicateWithFormat:@"self == %@", INPUT]];

答案 1 :(得分:2)

您应该尝试以下操作来搜索字符串开头的输入字符串:

NSPredicate *pred  = [NSPredicate predicateWithFormat:@"self BEGINSWITH %@", INPUT];
NSArray *searchRes = [self.allData filteredArrayUsingPredicate:pred];

或在任何地方搜索字符串:

NSPredicate *pred  = [NSPredicate predicateWithFormat:@"self CONTAINS %@", INPUT];
NSArray *searchRes = [self.allData filteredArrayUsingPredicate:pred];

您可能需要查看文档: https://developer.apple.com/library/mac/documentation/cocoa/conceptual/predicates/Articles/pUsing.html