SETUP
我有一个NSMutableDictionary,其中有超过800个NSMutableDictionaries代表员工。我正在尝试实现一个搜索栏,我正在处理与我的词典有关的严重问题。
在第一个For循环中,我创建了一个用于搜索的字典,并且在发送中我试图搜索该字典中的每个雇员。
问题
如何将单个词典添加到新词典中以容纳所有带有搜索词的词典?
- (void) searchTableView:(UISearchBar *)theSearchBar {
NSString *searchText = theSearchBar.text;
NSMutableDictionary *searchDict = [[NSMutableDictionary alloc] init];
for (NSDictionary *employee in employeeData)
{
[searchDict setValue:employee forKey:[employee objectForKey:kFULLNAME_TAG]];
}
for (NSDictionary *emp in searchDict)
{
NSString *empName = [emp objectForKey:kFULLNAME_TAG];
NSRange titleResultsRange = [empName rangeOfString:searchText options:NSCaseInsensitiveSearch];
if (titleResultsRange.length > 0){
NSLog(@"search result ---> %@" ,emp);
[copyListOfItems setValue:empName forKey:emp];
}
}
}
在第二个For循环中,我遇到了copyListOfItems setValue:empName forKey:emp的问题。
答案 0 :(得分:1)
我认为在插入copyListOfItems
时我会向后反转你的参数(我假设你的班级是一个NSMutableDictionary ivar?)。员工对象应该是值,员工姓名应该是关键。
[copyListOfItems setValue:emp forKey:empName];
你不应该使用两个循环来完成你需要的东西。这会更简单:
for (NSDictionary *emp in employeeData)
{
NSString *empName = [emp objectForKey:kFULLNAME_TAG];
NSRange titleResultsRange = [empName rangeOfString:searchText options:NSCaseInsensitiveSearch];
if (titleResultsRange.location != NSNotFound){
[copyListOfItems setValue:emp forKey:empName];
}
}