复制粘贴iOS学习笔记iOS 开发

iOS本地搜索之NSExpression

2016-06-11  本文已影响922人  船长_

1.场景需求

2.分析

关键方法

+ (NSPredicate *)predicateWithLeftExpression:(NSExpression *)lhs
                             rightExpression:(NSExpression *)rhs
                                    modifier:(NSComparisonPredicateModifier)modifier
                                        type:(NSPredicateOperatorType)type
                                     options:(NSUInteger)options

解析参数

lhs:左边的表达式。
rhs:右边的表达式。
modifier:应用的修改符。(ANY或者ALL)
type:谓词运算符类型。
options:要应用的选项。没有选项的话则为0。

NSComparisonPredicate选项

3.核心代码,封装成一个方法方便多个地方调用

-(NSArray*)filterWithString:(NSString*)text{
    if(!text||text.length<=0)
    {
        return self.arrayBak;
    }
    NSString *searchText =   text;//searchController.searchBar.text;
    NSMutableArray *searchResults = [self.arrayBak mutableCopy];

    // strip out all the leading and trailing spaces
    NSString *strippedString = [searchText stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];

    // break up the search terms (separated by spaces)
    NSArray *searchItems = nil;
    if (strippedString.length > 0) {
        searchItems = [strippedString componentsSeparatedByString:@" "];
    }

   // build all the "AND" expressions for each value in the searchString
    NSMutableArray *andMatchPredicates = [NSMutableArray array];
    
    for (NSString *searchString in searchItems) {

     NSMutableArray *searchItemsPredicate = [NSMutableArray array];

        NSExpression*lhs;
        NSExpression*rhs;
        NSPredicate*finalPredicate;
        {
            lhs = [NSExpression expressionForKeyPath:@"userName"];//名字
            rhs = [NSExpression expressionForConstantValue:searchString];
            finalPredicate = [NSComparisonPredicate
                                       predicateWithLeftExpression:lhs
                                       rightExpression:rhs
                                       modifier:NSDirectPredicateModifier
                                       type:NSContainsPredicateOperatorType
                                       options:NSCaseInsensitivePredicateOption];
        [searchItemsPredicate addObject:finalPredicate];
        }

       {
            lhs = [NSExpression expressionForKeyPath:@"job"];//职位
            rhs = [NSExpression expressionForConstantValue:searchString];
            finalPredicate = [NSComparisonPredicate
                              predicateWithLeftExpression:lhs
                              rightExpression:rhs
                              modifier:NSDirectPredicateModifier
                              type:NSContainsPredicateOperatorType
                              options:NSCaseInsensitivePredicateOption];
            [searchItemsPredicate addObject:finalPredicate];
        }

       {
            lhs = [NSExpression expressionForKeyPath:@"upCompany"];//职位
            rhs = [NSExpression expressionForConstantValue:searchString];
            finalPredicate = [NSComparisonPredicate
                              predicateWithLeftExpression:lhs
                              rightExpression:rhs
                              modifier:NSDirectPredicateModifier
                              type:NSContainsPredicateOperatorType
                              options:NSCaseInsensitivePredicateOption];
            [searchItemsPredicate addObject:finalPredicate];
        }

        NSNumber*targetNumber=@([searchString integerValue]*100);
        if (targetNumber != nil&&targetNumber.integerValue!=0) {   // searchString may not convert to a number
            lhs = [NSExpression expressionForKeyPath:@"price"]; 
            rhs = [NSExpression expressionForConstantValue:targetNumber];
            finalPredicate = [NSComparisonPredicate
                              predicateWithLeftExpression:lhs
                              rightExpression:rhs
                              modifier:NSDirectPredicateModifier
                              type:NSEqualToPredicateOperatorType
                              options:NSCaseInsensitivePredicateOption];
            [searchItemsPredicate addObject:finalPredicate];
 }

 // 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];

    return searchResults;
}

4.方法的使用

监听用户的输入的关键字调用封装的方法搜索,拿到搜索得到的数组刷新tableView

NSArray*searchResults=[self filterWithString:searchController.searchBar.text];
// hand over the filtered results to our search results table
FTFSearchResultsViewController   *tableController = (FTFSearchResultsViewController *)self.searchController.searchResultsController;
tableController.filteredResumeArray = searchResults;
[tableController.tableView reloadData];
上一篇下一篇

猜你喜欢

热点阅读