日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

通讯录排序

發布時間:2025/7/14 编程问答 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 通讯录排序 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

通訊錄是現在開發一個app必不可少的功能了,因為每個老板都想囤積用戶,所以都想搞IM,雖然這塊肉不是人人都能吃,但明顯很多人都想分一杯羹,我們作為開發,吃肉喝湯當然輪不到我們o(╯□╰)o,可是分下來的任務必須完成。

既然有通訊錄,當然離不開排序

  • 排序標記
@protocol SortProtocol <NSObject>/// 排序標記- (NSString *)sortPreferredFlag;@end 復制代碼
  • 外部調用接口

    /**分組排序@param objects 排序對象@param completion 排序結果@param empty 是否允許空數組@param queue 排序線程*/+ (void)sortObjects:(nullable NSArray<SortProtocol> *)objectswithCompletion:(void(^)(NSDictionary * _Nullable sortedResult, NSArray * _Nullable sectionTitles))completionwithEmptySections:(BOOL)empty withQueue:(nullable dispatch_queue_t)queue; 復制代碼
  • 內部實現

    • 創建一個SortPresenter類
+ (void)sortObjects:(NSArray<SortProtocol> *)objects withCompletion:(void (^)(NSDictionary * _Nullable, NSArray * _Nullable))completion withEmptySections:(BOOL)empty withQueue:(dispatch_queue_t)queue {if (!objects || objects.count == 0) { // 沒有可執行排序數據dispatch_async(dispatch_get_main_queue(), ^{completion(nil,nil);});} else {dispatch_queue_t currentQueue = queue ? queue : dispatch_queue_create("sortPresenter.queue", 0);dispatch_async(currentQueue, ^{[self invokeSortObjects:objects withEmptySections:empty withCompletion:completion];});} } 復制代碼+ (void)invokeSortObjects:(NSArray<SortProtocol> *)objects withEmptySections:(BOOL)empty withCompletion:(void (^)(NSDictionary * _Nullable,NSArray * _Nullable))completion {UILocalizedIndexedCollation *collation = [UILocalizedIndexedCollation currentCollation];NSInteger sectionTitlesCount = [[collation sectionTitles] count];NSMutableArray *sectionArray = [[NSMutableArray alloc] initWithCapacity:sectionTitlesCount];for (NSInteger index = 0; index < sectionTitlesCount; index ++) {NSMutableArray *section = [[NSMutableArray alloc] init];[sectionArray addObject:section];}for (id<SortProtocol> obj in objects) {NSInteger sectionIndex = [collation sectionForObject:obj collationStringSelector:@selector(sortPreferredFlag)];NSMutableArray *section = sectionArray[sectionIndex];[section addObject:obj];}NSMutableDictionary *resultDictionary = [NSMutableDictionary dictionaryWithCapacity:10];for (NSInteger index = 0; index < sectionArray.count; index ++) {NSMutableArray *section = sectionArray[index];NSArray *sortedSection = [collation sortedArrayFromArray:section collationStringSelector:@selector(sortPreferredFlag)];sectionArray[index] = sortedSection;}if (empty) {for (int index = 0; index < sectionArray.count && collation.sectionTitles.count; index ++) {[resultDictionary setObject:sectionArray[index] forKey:collation.sectionTitles[index]];}dispatch_async(dispatch_get_main_queue(), ^{completion(resultDictionary, collation.sectionTitles);});} else {NSMutableArray *fitterSectionArray = [NSMutableArray arrayWithCapacity:0];NSMutableArray *fitterSectionTitles = [NSMutableArray arrayWithCapacity:0];for (int index = 0; index < sectionArray.count && collation.sectionTitles.count; index ++) {if ([sectionArray[index] count] != 0) {[fitterSectionArray addObject:sectionArray[index]];[resultDictionary setObject:sectionArray[index] forKey:collation.sectionTitles[index]];[fitterSectionTitles addObject:collation.sectionTitles[index]];}}dispatch_async(dispatch_get_main_queue(), ^{completion(resultDictionary, fitterSectionTitles);});} } 復制代碼

調用只需要一句代碼:

[SortPresenter sortObjects:contactswithCompletion:^(NSDictionary * _Nullable sortedResult, NSArray * _Nullable sectionTitles) {self.dataSource = sortedResult.mutableCopy;self.sectionTitles = sectionTitles.mutableCopy;[self.tableView reloadData]; } withEmptySections:NO withQueue:nil]; 復制代碼

看看控制臺打印:

sortResult->>>>>>{A = ("<Contact: 0x60800003b840>");B = ("<Contact: 0x60800003b860>");C = ("<Contact: 0x608000227880>");H = ("<Contact: 0x60800003b580>","<Contact: 0x60800003b620>");K = ("<Contact: 0x60800003b600>");L = ("<Contact: 0x60800003b640>","<Contact: 0x60800003b540>");M = ("<Contact: 0x608000229980>");X = ("<Contact: 0x60800003b520>","<Contact: 0x60800003b4a0>","<Contact: 0x60800003b5a0>");Z = ("<Contact: 0x60800003b560>"); } 復制代碼

喜歡這個排序后的格式,條理清晰,又不會有太多冗余。 這個排序還是比較簡單,沒有做多音字和同音字處理,以后會完善。

轉載于:https://juejin.im/post/5a34eb786fb9a0452a3c6583

總結

以上是生活随笔為你收集整理的通讯录排序的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。