获取手机通讯录且排序

嗯,获取通讯录还有问题,使用静态检查发现有内存泄漏的问题就先不写了
先把排序加上去

//获取字符串(或汉字)首字母

  • (NSString *)firstCharacterWithString:(NSString *)string{
    if (string.length == 0) {
    return @"#";
    }
    NSMutableString *str = [NSMutableString stringWithString:string];
    CFStringTransform((CFMutableStringRef)str, NULL, kCFStringTransformMandarinLatin, NO);
    CFStringTransform((CFMutableStringRef)str, NULL, kCFStringTransformStripDiacritics, NO);
    NSString *pingyin = [str capitalizedString];
    return [pingyin substringToIndex:1];
    }

//将字符串数组按照元素首字母顺序进行排序分组

  • (NSMutableArray *)dictionaryOrderByCharacterWithOriginalArray:(NSArray *)array{
    if (array.count == 0) {
    return nil;
    }
    for (id obj in array) {
    if (![obj isKindOfClass:[NSString class]]) {
    return nil;
    }
    }
    UILocalizedIndexedCollation * indexedCollation = [UILocalizedIndexedCollation currentCollation];
    NSMutableArray *objects = [NSMutableArray arrayWithCapacity:indexedCollation.sectionTitles.count];
    //UILocalizedIndexedCollation 系统的东西还是很强大的
    for (int i = 0; i < indexedCollation.sectionTitles.count; i++) {
    NSMutableArray *obj = [NSMutableArray array];
    [objects addObject:obj];
    }
    //26个字母排序
    for (int i = 0; i < array.count; i++) {
    NSInteger index = [indexedCollation sectionForObject:array[i] collationStringSelector:@selector(uppercaseString)];
    [[objects objectAtIndex:index] addObject:array[i]];

    }
    return objects;
    }

//汉字的拼音

  • (NSString *)pinyin{
    NSMutableString *str = [self mutableCopy];
    CFStringTransform(( CFMutableStringRef)str, NULL, kCFStringTransformMandarinLatin, NO);
    CFStringTransform((CFMutableStringRef)str, NULL, kCFStringTransformStripDiacritics, NO);

    return [[str stringByReplacingOccurrencesOfString:@" " withString:@""] lowercaseString];
    }

//然后就是把每组中的排序
NSMutableArray * nameArr = [NSString dictionaryOrderByCharacterWithOriginalArray:array];
for (NSArray * arr in nameArr) {
NSMutableArray * array = [[NSMutableArray alloc]initWithArray:arr];
NSInteger count = array.count;
for (int i = 0; i < count; i++) {
for (int j = 0; j < count - i - 1; j++) {
//这里我是把每个字符串都转换成小写的拼音判断的
if([[array[j] pinyin] compare:[array[j+1] pinyin] options:NSForcedOrderingSearch] == 1){
[array exchangeObjectAtIndex:j withObjectAtIndex:(j + 1)];
}

        }
    }
    [_nameResultArr addObject:array];
}

PS:其实还有些没解决,比如张和章 排序出来可能就是张、章、张。。多音字也没做,慢慢研究吧。

好吧,一般这样了 产品也会向微信学习加个搜索吧...
这种模糊搜索什么的 还是用UISearchController吧
代理中

  • (void)updateSearchResultsForSearchController:(UISearchController *)searchController{
    NSLog(@"%@",searchController.searchBar.text);
    NSMutableArray * result = [[NSMutableArray alloc]init];
    for (NSArray * arr in _nameResultArr) {
    for (NSString * str in arr) {
    //这里是先判断输入的内容是否是汉字,如果是的话直接找,不是的话转成拼音找
    if ([NSString isHaveChineseInString:searchController.searchBar.text]) {
    if ([str rangeOfString:searchController.searchBar.text].location != NSNotFound) {
    [result addObject:str];
    }
    }else{
    NSString * textStr = [searchController.searchBar.text lowercaseString];
    if ([[str pinyin] rangeOfString:textStr].location != NSNotFound) {
    [result addObject:str];
    }
    }

      }
    

    }
    [_searchController.searchResult setArray:result];

    [_searchController reloadSearchResult];

}

//判断字符串中是否含有中文

  • (BOOL)isHaveChineseInString:(NSString *)string{
    for(NSInteger i = 0; i < [string length]; i++){
    int a = [string characterAtIndex:i];
    if (a > 0x4e00 && a < 0x9fff) {
    return YES;
    }
    }
    return NO;
    }

你可能感兴趣的:(获取手机通讯录且排序)