0

我有一个包含乌尔都语单词和英语单词的 plist。如下图所示

在此处输入图像描述

现在我的代码是获取数据如下

- (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"]];
    }
  }

这部分代码对我来说很好,就像我下面的屏幕截图一样在此处输入图像描述在此处输入图像描述

现在的问题是,当我通过搜索栏搜索任何单词时,它返回空结果,因为我的搜索数组包含不同格式的单词,我的搜索数组代码是

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",它表明我的数据以其他格式出现,这就是为什么搜索栏无法搜索它。任何帮助或建议将是得到了。谢谢

4

1 回答 1

1

plist 中的所有乌尔都语条目都有一个空格作为第一个字符。这就是搜索总是产生一个空列表的原因。

作为一种解决方法,可以在比较之前从条目中删除前导空格和尾随空格:

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];
    }
}
于 2013-01-24T20:45:47.773 回答