I have a table view and a search bar. I have to search for multiple keys' values and filter table accordingly. I use the following code to filter
- (void)updateSearchResultsForSearchController
{
NSString *searchText = self.searchBr.text;
NSMutableArray *searchResults = [self.arrayPriceList mutableCopy];
NSString *strippedString = [searchText stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSArray *searchItems = nil;
if (strippedString.length > 0) {
searchItems = [strippedString componentsSeparatedByString:@" "];
}
NSMutableArray *andMatchPredicates = [NSMutableArray array];
for (NSString *searchString in searchItems) {
NSMutableArray *searchItemsPredicate = [NSMutableArray array];
// Below we use NSExpression represent expressions in our predicates.
// NSPredicate is made up of smaller, atomic parts: two NSExpressions (a left-hand value and a right-hand value)
// Product SKU
NSExpression *lhsSKU = [NSExpression expressionForKeyPath:@"STOCKUNIT"];
NSExpression *rhsSKU = [NSExpression expressionForConstantValue:searchString];
NSPredicate *finalPredicateSKU = [NSComparisonPredicate
predicateWithLeftExpression:lhsSKU
rightExpression:rhsSKU
modifier:NSDirectPredicateModifier
type:NSContainsPredicateOperatorType
options:NSCaseInsensitivePredicateOption];
[searchItemsPredicate addObject:finalPredicateSKU];
// Product Major group
NSExpression *lhsProductMajorGroup = [NSExpression expressionForKeyPath:@"PRODUCTMAJORGROUP"];
NSExpression *rhsProductMajorGroup = [NSExpression expressionForConstantValue:searchString];
NSPredicate *finalPredicateMajorGroup = [NSComparisonPredicate
predicateWithLeftExpression:lhsProductMajorGroup
rightExpression:rhsProductMajorGroup
modifier:NSDirectPredicateModifier
type:NSContainsPredicateOperatorType
options:NSCaseInsensitivePredicateOption];
[searchItemsPredicate addObject:finalPredicateMajorGroup];
// at this OR predicate to our master AND predicate
NSCompoundPredicate *orMatchPredicates = [NSCompoundPredicate orPredicateWithSubpredicates:searchItemsPredicate];
[andMatchPredicates addObject:orMatchPredicates];
}
// match up the fields of the Product object
NSCompoundPredicate *finalCompoundPredicate =
[NSCompoundPredicate andPredicateWithSubpredicates:andMatchPredicates];
searchResults = [[searchResults filteredArrayUsingPredicate:finalCompoundPredicate] mutableCopy];
// hand over the filtered results to our search results table
self.arrayFilteredPriceList = searchResults;
}
But in cellForRowAtIndexPath, I have to get the value as below:
cell.lblSku.text = [[dict objectForKey:@"STOCKUNIT"] objectForKey:@"text"];
cell.lblProductMajor.text = [[dict objectForKey:@"PRODUCTMAJORGROUP"] objectForKey:@"text"];
My question is, how will I add objectForKey:@"text"
to get values for [NSExpression expressionForKeyPath:@"STOCKUNIT"];
? How to add in extra key to expressionForKeyPath
?
I solved it by adding .text in expressionForKeyPath
NSExpression *lhsProductName = [NSExpression expressionForKeyPath:@"PRODUCTNAME.text"];