// 01 创建可变字典,存储每一个key对应的数据列表 NSMutableDictionary *nationWithCodeDicList = [NSMutableDictionary dictionary]; // 02 遍历数组_dataList中的小字典 NSString *path = [[NSBundle mainBundle] pathForResource:@"nationcodenumber" ofType:@"plist"]; NSArray *_dataList = [NSArray arrayWithContentsOfFile:path]; for (NSDictionary *nationWithCodeDic in _dataList) { // 03 得到国家名称 // Model *model =[[Model alloc]init]; NSString *nationName = nationWithCodeDic[@"Name"]; // 04 转成可变字符串 NSMutableString *mutableNationName = [NSMutableString stringWithString:nationName]; // 05 先转换为带声调的拼音 CFStringTransform((CFMutableStringRef)mutableNationName,NULL, kCFStringTransformMandarinLatin,NO); // 06 再转换为不带声调的拼音 CFStringTransform((CFMutableStringRef)mutableNationName,NULL, kCFStringTransformStripDiacritics,NO); // 07 得到首字母之大写形式并设置为字典的一个key NSString *key = [mutableNationName substringToIndex:1].uppercaseString; NSLog(@"%@",key); // 08 获取nationWithCodeDicList中key对应的value(存放若干国家小字典的数组,国家名字的首字母都是key)nationWithCodeListKey NSMutableArray *nationWithCodeListKey = [nationWithCodeDicList objectForKey:key]; // 09 若此数组为空,则创建并保存到字典对应的key if (nationWithCodeListKey == nil) { nationWithCodeListKey = [NSMutableArray array]; [nationWithCodeDicList setObject:nationWithCodeListKey forKey:key]; } //MKMKMKMKMKMKMKMKMK Model *model =[Model currentModelWithDict:nationWithCodeDic]; // 10 若有此数组,就把本次遍历得到的数据(_dataList中的小字典)加入其中 [nationWithCodeListKey addObject:model]; // [nationWithCodeListKey addObject:nationWithCodeDic]; // 11 获取当前好友列表中所有的组标题 使用compare得到有顺序的字母数组 self.shouzimuarray = [[nationWithCodeDicList allKeys] sortedArrayUsingSelector:@selector(compare:)]; } self.dictionart = nationWithCodeDicList;