yretuta
yretuta

Reputation: 8111

Better Approach For The Results NSMutableArray in iOS search

So here's how I do the search:

  1. I have an array of Data and a mutable array of search results
  2. I perform the search
  3. I fill an array with the results of a search using a predicate with the filterdArrayUsingPredicate:
  4. 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!

Upvotes: 0

Views: 319

Answers (1)

Nikita Leonov
Nikita Leonov

Reputation: 5704

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.

Upvotes: 1

Related Questions