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:
- First I write John (results:John Newman, John Stable, John Carens)
- Then I write John with whitespace after (results:nothing)
- 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];
}