如何获得关键路径的NSExpression?

时间:2016-08-19 11:55:06

标签: ios objective-c 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添加额外的密钥?

1 个答案:

答案 0 :(得分:0)

我通过在expressionForKeyPath

中添加.text解决了这个问题
NSExpression *lhsProductName = [NSExpression expressionForKeyPath:@"PRODUCTNAME.text"];