1

I have been beating my head agains the wall on this one all day. I am new to Objective C and I know I am just making a stupid mistake so I would be really grateful for the help.

I have a an array loaded from a plist file of that contains a company directory. It includes the persons first name(fname), last names(lname), extention, phone number, title and department. The table view loads just fine, the object passes to the detail view without any issues, I have the search bar set up to filter a copy of the primary array, but I can't properly figure out how to search the array for the text entered into the bar. It really only needs to shearch the first name, last name, and while no necessary it would be cool to search the extension. All of this information appears in the cell like this, "John Appleseed" and the extension is in the subtitle below. This is the code I currently DON'T have working for the searchBar textDidChange method.

-(void) searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
if ([searchText length] == 0) {
    [filteredList removeAllObjects];
    [filteredList addObjectsFromArray:phoneListOriginal];
    NSLog(@"no change");
} else {
    savedSearchTerm = searchText;

   [filteredList removeAllObjects];
    for (NSDictionary *dict in phoneListOriginal) {
        for (NSString *testString in dict) {
            NSRange r = [testString rangeOfString:searchText options:NSCaseInsensitiveSearch];            
            if (r.location != NSNotFound) {
            [filteredList addObject:dict];
        }


        }
    }
    NSLog(@"%@",savedSearchTerm);
}
[self.tableView reloadData];

}

Is there a good way to search an array of dictionaries like this? I'm about to pull my hair out and I have a sneaking suspicion that I am just not seeing something really obvious.

Tenthrow
  • 51
  • 8
  • I ended up changing the array of dictionaries into an array of custom objects (stepping through one at a time to add them to a new array of objects), then searching with a predicate. This was much easier than I expected, and I even figure it out all by myself. – Tenthrow Apr 04 '12 at 19:55

0 Answers0