0
我有一個表格視圖和一個搜索欄。我必須搜索多個鍵的值並相應地篩選表。我使用下面的代碼過濾如何獲取關鍵路徑的NSExpression?
- (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;
}
不過的cellForRowAtIndexPath,我得爲以下值:
cell.lblSku.text = [[dict objectForKey:@"STOCKUNIT"] objectForKey:@"text"];
cell.lblProductMajor.text = [[dict objectForKey:@"PRODUCTMAJORGROUP"] objectForKey:@"text"];
我的問題是,我將如何添加objectForKey:@"text"
獲得值[NSExpression expressionForKeyPath:@"STOCKUNIT"];
?如何添加額外的密鑰到expressionForKeyPath
?