我有一个列表,其中包含乌尔都语单词,也有英语单词。如以下屏幕截图所示



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

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


当我Nslog listOfItems数组时,它显示文本,例如

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”,
它表明我的数据以其他形式输入,这就是为什么搜索栏无法搜索它的原因。
任何帮助或建议都将得到感谢。

最佳答案

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

关于iphone - 如何从plist提取乌尔都语单词?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14507408/

10-10 14:21