So here’s how I do the search:
- I have an array of Data and a mutable array of search results
- I perform the search
- I fill an array with the results of a search using a predicate with the filterdArrayUsingPredicate:
-
add that array to the mutable array of results via
addObjectsFromArray:as such:- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope { NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"SELF contains[cd] %@", searchText]; NSArray *results = [self.allItems filteredArrayUsingPredicate:resultPredicate]; NSMutableArray *myResults = [[NSMutableArray alloc] init]; [myResults addObjectsFromArray:results]; self.searchResults = myResults; [myResults release]; NSLog(@"%@", self.searchResults); }
The problem I see is that I have to create a mutable array everytime a search is performed, which is whenever a “keyup” occurs. I was wondering if there was a better approach with this one.
Thanks in advance!
Step #4 is not makes sense. Assign filtered array directly but not thru myResults. In case of some internal logic you will need a copy of filtered results array you still can get it my calling copy method.