Search code examples
iosobjective-ciphonensmutablearraynspredicate

Filter Array of custom objects using NSPredicate in Objective C


I have one array which contains dictionary of custom objects.I want to filter that array for search functionality in UITableView. Here is my code for single object Filtration using NSPredicate.

self.searchResultForName = [self.multiPracticeDetailsArray filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) {
    ObjectHolder *obj = (ObjectHolder*)evaluatedObject;
    return [obj.Name hasPrefix:searchText];
}]];

Using above code, I can search only for Name , but i want to search for both By Name and ID.Also search will be case insensitive. How can i do this in Objective-C?.Please Suggest any better way to do this.


Solution

  • Did you tried with || OR operator & lowercaseString.

    self.searchResultForName = [self.multiPracticeDetailsArray filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) {
        ObjectHolder *obj = (ObjectHolder*)evaluatedObject;
        return [[obj.Name lowercaseString] hasPrefix:[searchText lowercaseString]] || [[obj.ID lowercaseString] hasPrefix:[searchText lowercaseString]] ;
    }]];