2016-08-19 85 views
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

回答

0

我解决它在expressionForKeyPath

NSExpression *lhsProductName = [NSExpression expressionForKeyPath:@"PRODUCTNAME.text"]; 
加入的.text