本地搜索--SearchBar--干货

为了节省大家的时间直接上gif

本地搜索--SearchBar--干货_第1张图片
demo

工程框架是用sb部署的,所以有些细节没处理好.
我把自己封装的工具类发上来.
整个demo我已经传到GitHub上了,有需要的请下载,可以star┗|`O′|┛ 嗷~~
传送门 https://github.com/JonesCxy/search

#import "NDSearchTool.h"

@implementation NDSearchTool


+(NDSearchTool *)tool{

    NDSearchTool *tool = [[NDSearchTool alloc]init];
    
    return tool;

}


- (NSArray *)searchWithFieldArray:(NSArray *)fieldArray
                      inputString:(NSString *)inputString
                          inArray:(NSArray *)array
{
    if (![array count] || ![fieldArray count]) {
        return nil;
    }
    
    NSPredicate *scopePredicate;
    NSMutableArray *backArray = [NSMutableArray array];
    
    for (NSString *fieldString in fieldArray) {
        
        scopePredicate = [NSPredicate predicateWithFormat:@"SELF.%@ contains[c] %@", fieldString, inputString];
        NSArray *tempArray = [array filteredArrayUsingPredicate:scopePredicate];
    
        for (NSObject *object in tempArray) {
            if (![backArray containsObject:object]) {
                [backArray addObject:object];
            }
        }
    }
    
    return backArray;
}


-(NSArray *)searchWithFieldArray:(NSArray *)allFieldArray
                     inputString:(NSString *)inputString
                      inAllArray:(NSArray *)allArray{


    NSInteger count = allArray.count;
    if (allFieldArray.count != allArray.count || 0 == count) {
        
        return nil;
    }
    
    NSMutableArray *backArray = [NSMutableArray array];
    
    
    for (NSInteger i = 0; i < count; i++) {
        
        NSArray *tempArray = [self searchWithFieldArray:allFieldArray[i] inputString:inputString inArray:allArray[i]];
        
        [backArray addObject:tempArray];
    }

    return backArray;
}


你可能感兴趣的:(本地搜索--SearchBar--干货)