iphone - 如何从 plist 中获取乌尔都语单词?

标签 iphone ios xcode escaping uisearchbardisplaycontrol

我有一个包含乌尔都语单词和英语单词的 plist。如下面的屏幕截图

enter image description here

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

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

这部分代码对我来说效果很好,如下面的屏幕截图 enter image description here enter image description here

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

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/

相关文章:

ios - .xib 文件如何自动关联并加载到我的 ViewController?

ios - swift 3 在应用程序处于后台时读取加速度计数据

iphone - 是否可以在 iOS 的 AddThis API 的共享菜单中覆盖添加按钮调用的函数?

iphone - 是否可以配置 UITableView 以允许多重选择?

ios - 延迟加载 UICollectionViewCell 的自定义 subview

ios - 自定义 iOS UINavigationBar/UITabBar 和移除 Tint Colors

ios - 如何创建保留在 ipad 上的多列表头

ios - 为什么我的 iOS 应用程序在我从 iOS 模拟器打开时失败/关闭?

iphone - 没有可见的@interfacefor 'NSNumber' 声明选择器 initwithInt。错误

iOS:当我询问 Siri "What can you do?"时,Siri 没有列出我的 iOS 支付应用程序