1

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];
}
  • Whne you have the trailing space, you get an empty string as second value in `array`. You need to deal with that special case. Or better yet, trim whitespace from the beginning and end of you search string (because it'll also fail if you start with a space), see http://stackoverflow.com/questions/5756256/trim-spaces-from-end-of-a-nsstring – Gerd K May 11 '17 at 00:36
  • Hello, thank you for answer. Can you give me some example in code please? Thank you very much @GerdK – Milan Worsch May 12 '17 at 20:05

1 Answers1

1

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.

kb920
  • 3,039
  • 2
  • 33
  • 44
  • Thank you for answer, I currently don`t know where to put call for method to refresh search, you did it with text field. Please can you check my edit? – Milan Worsch May 11 '17 at 14:04
  • @MilanWorsch I have updated my answer. Check it. If it's work then accept or upvote to my solution – kb920 May 11 '17 at 17:34
  • Now it is the same code as I wrote at the beginning in my question. If I write in search bar "John ", no results – Milan Worsch 10 mins ago – Milan Worsch May 11 '17 at 22:27