Milan Worsch
Milan Worsch

Reputation: 19

Search by using NSPredicate - Full name

It`s been almost 3 days and I still can not fix the bug. Simple situation, I would like to search for name of person (Name and surname).

Example:

  1. First I write John (results:John Newman, John Stable, John Carens)
  2. Then I write John with whitespace after (results:nothing)
  3. After that I continue with John N (results:John Newman). I need to keep names displayed even if I write whitespace in the search.

It is just about the first part of if/else where I work with array of 2+ words. Thanks


- (void)searchForText:(NSString*)searchText
{
NSString *searchTextt = [searchText stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];

        NSArray *array = [searchTextt componentsSeparatedByString:@" "];


        NSString *firstName = searchTextt;
        NSString *lastName = searchTextt;
        NSString *firstName2 = searchTextt;
        NSString *lastName2 = searchTextt;

        NSPredicate *predicate = nil;

    if ([array count] > 1) {
        firstName = array[0];
        lastName = array[1];
        firstName2 = array[1];
        lastName2 = array[0];

        predicate = [NSPredicate predicateWithFormat:@"(guestName CONTAINS[cd] %@ AND guestSurname CONTAINS[cd] %@)", firstName, lastName];
        NSPredicate *predicate2 = [NSPredicate predicateWithFormat:@"(guestSurname CONTAINS[cd] %@ AND guestName CONTAINS[cd] %@)", lastName2, firstName2];

        NSPredicate *compoundPredicate = [NSCompoundPredicate orPredicateWithSubpredicates:@[predicate, predicate2]];


        self.searchResults = [self.mainarray filteredArrayUsingPredicate:compoundPredicate];

    } else {
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"guestSurname contains[cd] %@", searchText];
        NSPredicate *predicate2 = [NSPredicate predicateWithFormat:@"guestName contains[cd] %@", searchText];
        NSPredicate *predicate3 = [NSPredicate predicateWithFormat:@"guestCompany contains[cd] %@", searchText];


        NSPredicate *compoundPredicate = [NSCompoundPredicate orPredicateWithSubpredicates:@[predicate, predicate2, predicate3]];
        self.searchResults = [self.mainarray filteredArrayUsingPredicate:compoundPredicate];

    }


}

EDIT:

- (void)updateSearchResultsForSearchController:(UISearchController *)searchController
{
    NSString *searchString = searchController.searchBar.text;
    [self searchForText:searchString];
    [_maintable reloadData];
}

Upvotes: 1

Views: 865

Answers (1)

kb920
kb920

Reputation: 3089

I have an array like

( { guestName = John; guestSurname = Newman; }, { guestName = John; guestSurname = Stable; }, { guestName = John; guestSurname = Carens; } )

I write fuction

- (void)searchForText:(NSString*)searchText 
    {
        searchTextt = [searchText stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
        NSArray *array = [searchTextt componentsSeparatedByString:@" "];
        NSString *firstName = searchTextt;
        NSString *lastName = searchTextt;
        NSPredicate *predicate = nil;

        if ([array count] > 1) {
            firstName = array[0];
            lastName = array[1];
            predicate = [NSPredicate predicateWithFormat:@"(guestName CONTAINS[cd] %@ AND guestSurname CONTAINS[cd] %@) OR (guestName CONTAINS[cd] %@ AND guestSurname CONTAINS[cd] %@)", firstName, lastName, lastName, firstName];

        } else {
            predicate = [NSPredicate predicateWithFormat:@"guestName CONTAINS[cd] %@ OR guestSurnamen CONTAINS[cd] %@", firstName, lastName];

        }
        NSArray *arrResult =  [arr filteredArrayUsingPredicate:predicate];;

    }

- (void)updateSearchResultsForSearchController:(UISearchController 
 *)searchController
{
    NSString *searchString = searchController.searchBar.text;
    [self searchForText:searchString];
    [_maintable reloadData];
}

Try this. It's work for me I am getting result when I enter "john " in textfield.

Upvotes: 1

Related Questions