有些人可以帮助我吗
我在应用启动应用时加载了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);
答案 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];