美文网首页
[iOS]地址排序&获取汉字首字母 例如:北京市->bjs

[iOS]地址排序&获取汉字首字母 例如:北京市->bjs

作者: 汴城码农 | 来源:发表于2022-08-25 13:33 被阅读0次
    -(NSArray*)AZIndexDispose:(NSArray *)array{
        NSMutableArray *allArray = @[].mutableCopy;
        for (NSString  *character in  _indexArray) {
            NSMutableArray *itemArray = @[].mutableCopy;
            
            for (NSDictionary *dict in array) {
                NSString *name = [dict stringValueForKey:@"name"];
                NSString *firstStr = [self firstCharactor:name];
                if ([character isEqualToString:firstStr]) {
                    [itemArray addObject:dict];
                }
            }
            [allArray addObject:itemArray];
        }
        
        return allArray;
    }
    
    - (NSString *)firstCharactor:(NSString *)aString
    {
        NSMutableString *strSum = [[NSMutableString alloc]init];
        for (int i = 0 ; i < 1 ; i++) {
            //转成了可变字符串
            NSMutableString *str = [NSMutableString stringWithString:[aString substringWithRange:NSMakeRange(i, 1)]];
            NSLog(@"---%@",str);
            //先转换为带声调的拼音
            CFStringTransform((CFMutableStringRef)str,NULL, kCFStringTransformMandarinLatin,NO);
            //再转换为不带声调的拼音
            CFStringTransform((CFMutableStringRef)str,NULL, kCFStringTransformStripDiacritics,NO);
            //转化为大写拼音
            NSString *pinYin = [str lowercaseString];
            NSLog(@"---%@",pinYin);
            //获取并返回首字母
            strSum = [[NSMutableString alloc]initWithString:[strSum stringByAppendingString:[pinYin substringToIndex:1]]];
        }
        NSLog(@"拼音%@",strSum);
        return [strSum uppercaseString];
    }
    

    相关文章

      网友评论

          本文标题:[iOS]地址排序&获取汉字首字母 例如:北京市->bjs

          本文链接:https://www.haomeiwen.com/subject/ysoagrtx.html