我有一个plist,其中包含urdu语言单词,并且还有英语单词。如下面的截图
现在我的代码是获取数据,如下所示
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[words removeAllObjects];
[means removeAllObjects];
NSString *path = [[NSBundle mainBundle] pathForResource:
@"urdutoeng" ofType:@"plist"];
NSMutableArray *array2 = [[NSMutableArray alloc] initWithContentsOfFile:path];
for (int i=0; i<[array2 count]; i++) {
NSDictionary* dict = [array2 objectAtIndex:i];
[words addObject:[dict valueForKey:@"Urdu"]];
[means addObject:[dict valueForKey:@"English"]];
[Types addObject:[dict valueForKey:@"Nature"]];
}
}
我的下方屏幕截图
,这部分代码可以正常使用现在的问题是当我通过搜索栏搜索任何单词时它返回空结果beacuse我的搜索数组包含不同的Formate中的单词我的搜索数组代码是
listOfItems = [[NSMutableArray alloc] init];
NSDictionary *countriesToLiveInDict = [NSDictionary dictionaryWithObject:words forKey:@"Countries"];
[listOfItems addObject:countriesToLiveInDict];
copyListOfItems = [[NSMutableArray alloc] init];
我的搜索栏代码是
#pragma mark Content Filtering
- (void)filterContentForSearchText:(NSString*)searchText
{
[copyListOfItems removeAllObjects];
NSLog(@"listOfItemsdata :%@",listOfItems);
for (NSString *cellLabel in [[listOfItems objectAtIndex:0] objectForKey:@"Countries"])
{
NSComparisonResult result = [cellLabel compare:searchText options: (NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, [searchText length])];
if (result == NSOrderedSame)
{
[copyListOfItems addObject:cellLabel];
}
}
}
#pragma mark UISearchDisplayController Delegate Methods
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
NSLog(@"searchstring :%@",searchString);
[self filterContentForSearchText:searchString];
return YES;
}
当我对listOfItems数组进行Nslog时,它会显示一些类似
的文本NSLog(@“listOfItemsdata:%@”,listOfItems);
listOfItemsdata :( { 国家=( “\ U0627 \ U0628”, “\ U0627 \ U0628 \ U0628 \ U06be \ U06cc”, “\ U0627 \ U0628 \ U062a \ U0628”, “\ U0627 \ U0628 \ U062a \ U06a9”, “\ U0627 \ U0628 \ U062c \ U0628 \ U06a9 \ U06c1”, “\ U0627 \ U0628 \ U0633 \ U06d2”, 它显示我的数据进入其他一些形式,这就是为什么搜索栏无法搜索它。 将提供任何帮助或建议。谢谢
答案 0 :(得分:1)
plist中的所有Urdu条目都有一个空格作为第一个字符。这就是搜索总是产生一个空列表的原因。
作为一种解决方法,可以在比较前删除条目中的前导和尾随空格:
for (NSString *cellLabel in [[listOfItems objectAtIndex:0] objectForKey:@"Countries"])
{
NSString *trimmed = [cellLabel stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSComparisonResult result = [trimmed compare:searchText options: (NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, [searchText length])];
if (result == NSOrderedSame)
{
[copyListOfItems addObject:cellLabel];
}
}