iphone - 从字典中的数组中删除与搜索词匹配的所有值(字符串)?

标签 iphone ios uitableview nsdictionary uisearchbar

目前我正在编写一个带有 tableView 的应用程序,类似于 iPhone 通讯录应用程序中的应用程序。

一切正常(部分,右侧显示标题的栏,单元格已配置......),在搜索栏旁边。如果 tableView 的数据是从数组加载的,我很熟悉如何执行此操作(搜索),但我的情况是它是从位于 NSDictionary 中的数组加载的。

字典看起来像

Key = "A" >> Value = "array = apple, animal, alphabet, abc ..."
Key = "B" >> Value = "array = bat, ball, banana ..."

如何删除与搜索词匹配的所有字符串(从字典的所有数组中)?

提前致谢:)

最佳答案

你可以这样做

NSMutableDictionary *newItems = [NSMutableDictionary dictionary];
for (NSString *key in oldItems) {
    NSMutableArray *newArray = [NSMutableArray array];
    for (NSString *item in [oldItems objectForKey:key]) {
        if ([item rangeOfString:searchTerm].location != NSNotFound) {
            [newArray addObject:item];
        }
    }
    if ([newArray count]) {
        [newItems setObject:newArray forKey:key];
    }
}
[oldItems release];
oldItems = [newItems retain];

我不知道这是否是最好的方法,或者即使它足够快,但请告诉我这是否适合您。

关于iphone - 从字典中的数组中删除与搜索词匹配的所有值(字符串)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6976515/

相关文章:

iphone - 在 XCode 4 中更改项目的不同 SVN 存储库

ios - 带有部分的快速表格 View 无法使用搜索栏

ios - 自定义 UITableView - iphone

iphone - 如何在 xcode 中检查 NSArray 和 NSDictionary 中的元素?

ios - 为 NSData iOS 中的每一位设置数据

iPhone - 显示 "location need to be available"对话框

Swift:从自定义 UITableViewCell 中的 UIButton 更新 UITableView

ios - TableView HeaderSections 隐藏按钮

iphone - 使用 HTTPS 的 NSURLRequest

ios - iPhone 上的文件夹和图像在哪里?[swift]