Daniel Fernandez Daniel Fernandez - 1 month ago 25
iOS Question

Case insensitive SearchBar Xcode

I'm developing an app in Xcode in Objective-C. The app has a TableView with restaurants. My problem is that when I use the SearchBar the filter is case sensitive and I want to get a case insensitive search.
I know I have to change my

searchBar textDidChange
method, and I looking for help but I don't know how to change it in my case. Can someone help me? Thank you very much.

This is my
searchBar textDidChange
method:

-(void) searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {

if (searchText.length == 0) {
//Set our boolean flag
isFiltered = NO;
} else {
//Set our boolean flag
isFiltered = YES;
}
//Alloc and init our filteredData
filteredRest = [[NSMutableArray alloc] init];

for (Restaurant *item in originData) {
if ([item.title containsString:searchText]) {
[filteredRest addObject:item];
}
}

//Reload our table view
[self.tableView reloadData];
}


Thank you.

Answer

Change this line

if ([item.title containsString:searchText]) {
    [filteredRest addObject:item];
}

With this

if ([[item.title lowercaseString]containsString:[searchText lowercaseString]]) {
    [filteredRest addObject:item];
}