[iOS]地址排序&获取汉字首字母 例如:北京市->bjs
2022-08-25 本文已影响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];
}