我有一个包含乌尔都语单词和英语单词的 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(@"listOfItemsdata :%@",listOfItems);
项目数据列表:( { 国家 = ( “\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 中的所有乌尔都语条目都以空格作为第一个字符。这就是搜索总是产生空列表的原因。
作为一种解决方法,可以在比较之前删除条目中的前导空格和尾随空格:
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/