XYCountryCodeUtils.m 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. //
  2. // XYCountryCodeUtils.m
  3. // XYCountryCode
  4. //
  5. // Created by 杨卢银 on 2018/8/16.
  6. // Copyright © 2018年 杨卢银. All rights reserved.
  7. //
  8. #import "XYCountryCodeUtils.h"
  9. @interface XYCountryCodeUtils ()
  10. @end
  11. @implementation XYCountryCodeUtils
  12. static XYCountryCodeUtils* _instance = nil;
  13. +(XYCountryCodeUtils *)shareUtils
  14. {
  15. static dispatch_once_t onceToken ;
  16. dispatch_once(&onceToken, ^{
  17. _instance = [[XYCountryCodeUtils alloc] init] ;
  18. }) ;
  19. return _instance ;
  20. }
  21. -(instancetype)init{
  22. self = [super init];
  23. if (self) {
  24. [self buildData];
  25. }
  26. return self;
  27. }
  28. -(void)buildData{
  29. // 设置文件路径
  30. NSString *bundlePath = [[NSBundle mainBundle]pathForResource:@"XYCountryCode"ofType:@"bundle"];
  31. NSBundle *resourceBundle = [NSBundle bundleWithPath:bundlePath];
  32. NSString *jsonPath = [resourceBundle pathForResource:@"Data/XYCountryCode" ofType:@"json"];
  33. _countryArray = [NSMutableArray array];
  34. NSData *data = [NSData dataWithContentsOfFile:jsonPath];
  35. NSError *error;
  36. NSArray *list = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
  37. for (NSDictionary *info in list) {
  38. XYCountry *c = [[XYCountry alloc] initWithDictionary:info];
  39. [_countryArray addObject:c];
  40. }
  41. }
  42. -(NSArray<XYCountryManager *> *)managers{
  43. return [self managersByShowType:XYCountryShowTypeZH];
  44. }
  45. -(NSArray<XYCountryManager *> *)managersByShowType:(XYCountryShowType)aType{
  46. NSMutableDictionary *listdic = [NSMutableDictionary dictionary];
  47. if (_countryArray && _countryArray.count>0) {
  48. for (XYCountry *country in self.countryArray) {
  49. NSString *firstZ = [self firstCharactorWithString:country.en];
  50. if (aType == XYCountryShowTypeZH) {
  51. firstZ = [self firstCharactorWithString:country.zh];
  52. }
  53. if (aType == XYCountryShowTypeTW) {
  54. firstZ = [self firstCharactorWithString:country.tw];
  55. }
  56. NSMutableArray *list = listdic[firstZ];
  57. if (!list) {
  58. list = [NSMutableArray array];
  59. [listdic setObject:list forKey:firstZ];
  60. }
  61. [list addObject:country];
  62. }
  63. }
  64. NSMutableArray *list = [NSMutableArray array];
  65. for (NSString *key in listdic.allKeys) {
  66. NSArray *l = listdic[key];
  67. XYCountryManager *m = [[XYCountryManager alloc] init];
  68. m.title = key;
  69. m.countrys = l;
  70. [list addObject:m];
  71. }
  72. NSArray *resultArray = [list sortedArrayUsingComparator:^NSComparisonResult(XYCountryManager *obj1, XYCountryManager *obj2) {
  73. return [obj1.title compare:obj2.title];
  74. }];
  75. return resultArray;
  76. }
  77. //获取某个字符串或者汉字的首字母.
  78. - (NSString *)firstCharactorWithString:(NSString *)string
  79. {
  80. NSMutableString *str = [NSMutableString stringWithString:string];
  81. CFStringTransform((CFMutableStringRef) str, NULL, kCFStringTransformMandarinLatin, NO);
  82. CFStringTransform((CFMutableStringRef)str, NULL, kCFStringTransformStripDiacritics, NO);
  83. NSString *pinYin = [str capitalizedString];
  84. return [pinYin substringToIndex:1];
  85. }
  86. -(NSArray<XYCountryManager *> *)buildManagers:(NSArray<XYCountry *> *)countrys showType:(XYCountryShowType)aType{
  87. NSMutableDictionary *listdic = [NSMutableDictionary dictionary];
  88. if (countrys && countrys.count>0) {
  89. for (XYCountry *country in countrys) {
  90. NSString *firstZ = [self firstCharactorWithString:country.en];
  91. if (aType == XYCountryShowTypeZH) {
  92. firstZ = [self firstCharactorWithString:country.zh];
  93. }
  94. if (aType == XYCountryShowTypeTW) {
  95. firstZ = [self firstCharactorWithString:country.tw];
  96. }
  97. NSMutableArray *list = listdic[firstZ];
  98. if (!list) {
  99. list = [NSMutableArray array];
  100. [listdic setObject:list forKey:firstZ];
  101. }
  102. [list addObject:country];
  103. }
  104. }
  105. NSMutableArray *list = [NSMutableArray array];
  106. for (NSString *key in listdic.allKeys) {
  107. NSArray *l = listdic[key];
  108. XYCountryManager *m = [[XYCountryManager alloc] init];
  109. m.title = key;
  110. m.countrys = l;
  111. [list addObject:m];
  112. }
  113. NSArray *resultArray = [list sortedArrayUsingComparator:^NSComparisonResult(XYCountryManager *obj1, XYCountryManager *obj2) {
  114. return [obj1.title compare:obj2.title];
  115. }];
  116. return resultArray;
  117. }
  118. @end