views:

114

answers:

1

hello there and sorry for the stupid question but i think i might be missing something simple here and can t figure it out myself.

i m trying to search a table view using the following code:

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{

    NSString *predicateFormat = @"(name contains[c] %@) OR (age contains[c] %@)";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:predicateFormat, searchString];
    self.fetchedResultsController = [self resetFetchedResultsController:predicate cached:NO];

    NSError *error = nil;
    [self.fetchedResultsController performFetch:&error];

    // Return YES to cause the search result table view to be reloaded.
    return YES;
}

what i want to achieve is when the user searches the table to be able to search not only by "name" but with "age" as well!

my code above only searches the "name"

Am i missing something simple?

thank you for your time

+1  A: 

Instead of:

NSString *predicateFormat = @"(name contains[c] %@) OR (age contains[c] %@)";
NSPredicate *predicate = [NSPredicate predicateWithFormat:predicateFormat, searchString];

You have three options:

NSString *predicateFormat = @"(name contains[c] %@) OR (age contains[c] %@)";
NSPredicate *predicate = [NSPredicate predicateWithFormat:predicateFormat, searchString, searchString];

Or:

NSString *predicateFormat = @"(name contains[c] %1$@) OR (age contains[c] %1$@)";
NSPredicate *predicate = [NSPredicate predicateWithFormat:predicateFormat, searchString];

Or:

NSString *predicateFormat = @"(name contains[c] $SEARCH) OR (age contains[c] $SEARCH)";
NSPredicate *template = [NSPredicate predicateWithFormat:predicateFormat];
NSPredicate *predicate = [template predicateWithSubstitutionVariables:[NSDictionary dictionaryWithObject:searchString forKey:@"SEARCH"]];

The 3rd option is nice if you're building a large predicate that has an unknown number of substitutions. The 2nd is probably the simplest in terms of not repeating yourself, but the 1st is easiest to understand.

Dave DeLong
thank you! that is great! i appreciate your time to answer my question!
treasure
treasure: To clarify: The original code uses two format specifiers but only provides one object. This not only doesn't work, but may crash. The first solution provides as many arguments as format specifiers; the second solution provides one argument and refers to the first argument from both format specifiers; the third uses a dictionary instead of an argument list and refers to the argument by name (dictionary key).
Peter Hosey
thank you! when you re quite new at this and never had proper training it is difficult to code and understand what is going on behind every line of code. i feel like a blind man feeling his way around... However with lots of help from developers, apple engineers, etc i have started to "see" some light at the end of the tunnel. I got VERY long way ahead of me but i m not giving up neither stop reading and asking questions. Thank you!
treasure