模糊搜索

模糊搜索在许多APP中都有应用到,在这里特意整理了下思路.在模糊搜索里,涉及到拼音和英文的搜索......
以下是核心代码:
 
  

//拼音

-(NSString*)transformToPinyin{

    NSMutableString *mutableString=[NSMutableString stringWithString:self];

    CFStringTransform((CFMutableStringRef)mutableString,NULL,kCFStringTransformToLatin,false);

    mutableString = (NSMutableString*)[mutableString stringByFoldingWithOptions:NSDiacriticInsensitiveSearch locale:[NSLocale currentLocale]];

   mutableString = [[mutableString stringByReplacingOccurrencesOfString:@" " withString:@""] mutableCopy];

    return mutableString.lowercaseString;

}

- (NSString *)transformToPinyinFirstLetter {

    NSMutableString *stringM = [NSMutableString string];

    

    

    NSString *temp  =  nil;

    for(int i =0; i < [self length]; i++){

        

        temp = [self substringWithRange:NSMakeRange(i, 1)];

        

        NSMutableString *mutableString=[NSMutableString stringWithString:temp];

        

        CFStringTransform((CFMutableStringRef)mutableString,NULL,kCFStringTransformToLatin,false);

        

        mutableString = (NSMutableString*)[mutableString stringByFoldingWithOptions:NSDiacriticInsensitiveSearch locale:[NSLocale currentLocale]];

        

        mutableString =  [[mutableString substringToIndex:1] mutableCopy];

        

        [stringM appendString:(NSString *)mutableString];

        

    }

    

    return stringM.lowercaseString;

}

-(void)startSearch:(NSString *)string{

    if (self.seachUserDTOList.count>0) {

        [self.seachUserDTOList removeAllObjects];
    }

    //开始搜索
    NSString *key = string.lowercaseString;
    NSMutableArray *tempArr = [NSMutableArray array];

    // NSLog(@"key = %@",key);

    if (![key isEqualToString:@""] && ![key isEqual:[NSNull null]] && key != nil) {

        [self.storeUserDTOList enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {

            UserDTO *dto = self.storeUserDTOList[idx];

            //担心框架有时候会误转,再次都设置转为小写
            NSString *name = dto.name.lowercaseString;
            NSString *namePinyin = dto.namePinYin.lowercaseString;
            NSString *nameFireLetter = dto.nameFirstLetter.lowercaseString;

            NSRange rang1 = [name rangeOfString:key];
            if (rang1.length>0) { ///比嫩 -比

                [tempArr addObject:dto];
            }else{

                if ([nameFireLetter containsString:key]) { //bn - b

                    [tempArr addObject:dto];

                }else{  //ershou

                    if ([nameFireLetter containsString:[key substringToIndex:1]]) {

                        if ([namePinyin containsString:key] ) {
                            [tempArr addObject:dto];
                        }
                    }
                }
            }
        }];

        [tempArr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {

            if (![self.seachUserDTOList containsObject:tempArr[idx]]) {

                [self.seachUserDTOList addObject:tempArr[idx]];
            }
        }];

        //NSLog(@"self.searchResultList  = %@",self.seachUserDTOList);
        self.isSearchState = YES;
    }else{

        self.isSearchState = NO;
    }

    [self.tableView reloadData];
}

你可能感兴趣的:(模糊搜索)