NSMutableArray *groupSameElementsInReverseconcon(NSMutableArray *bigArray) {
NSMutableArray *resultGroups = [NSMutableArray array];
if (bigArray.count == 0) {
return resultGroups;
}
// 从最后一个元素开始倒序遍历
for (NSInteger i = (NSInteger)bigArray.count - 1; i >= 0; i--) {
DBB_ContactModel *currentElement = bigArray[i];
BOOL elementAddedToGroup = NO;
for (NSMutableArray *group in resultGroups) {
DBB_ContactModel *objModel = group[0];
if (![objModel.name isEqualToString:@" "] && ![currentElement.name isEqualToString:@" "] && [objModel.name isEqualToString:currentElement.name]) {
[group addObject:currentElement];
elementAddedToGroup = YES;
break;
}else{
NSInteger i_Count = currentElement.numberArray.count;
NSInteger J_Count = objModel.numberArray.count;
if (i_Count>0 && J_Count>0) {
if (i_Count>=J_Count) {
for (NSString *phoneNum in objModel.numberArray) {
if ([currentElement.numberArray containsObject:phoneNum]) {
[group addObject:currentElement];
elementAddedToGroup = YES;
break;
}
}
}else{
for (NSString *phoneNum in currentElement.numberArray) {
if ([objModel.numberArray containsObject:phoneNum]) {
[group addObject:currentElement];
elementAddedToGroup = YES;
break;
}
}
}
}
}
}
if (!elementAddedToGroup) {
NSMutableArray *newGroup = [NSMutableArray arrayWithObject:currentElement];
[resultGroups addObject:newGroup];
}
}
return resultGroups;
}
//调用:
NSMutableArray *erweiShuzu = groupSameElementsInReverseconcon(array);
// 找出数组中 所有相同的电话号码
for (int i=0; i<erweiShuzu.count; i++){
NSMutableArray *subArray = erweiShuzu[i];
if (subArray.count>1) {
/*
1.遍历subArray
2.把合并结果插入subArray第一位
*/
NSMutableArray *hebingArray = [NSMutableArray array];
DBB_ContactModel *setModel = [[DBB_ContactModel alloc] init];
setModel.isSelected = NO;
for (int s =0;s<subArray.count;s++) {
DBB_ContactModel *model = subArray[s];
if (model.numberArray.count>0) {
for (NSString *dbb_number in model.numberArray) {
// 合并后 的代码
if (![hebingArray containsObject:dbb_number]) {
[hebingArray addObject:dbb_number];
}
}
}
if (s==0) {
if (![model.name isEqualToString:@" "] && model.name.length>0) {
setModel.name = model.name;
}
}
}
if (!setModel.name) {
DBB_ContactModel *lastModel = subArray.lastObject;
setModel.name = lastModel.name;
}
setModel.numberArray = hebingArray;
CNMutableContact *obj = [[CNMutableContact alloc] init];
obj.familyName = setModel.name;
NSMutableArray *phoneNums = [NSMutableArray array];
for (NSString *phoneStr in hebingArray) {
CNPhoneNumber *number = [CNPhoneNumber phoneNumberWithStringValue:phoneStr];
CNLabeledValue *phone = [[CNLabeledValue alloc]initWithLabel:CNLabelPhoneNumberiPhone value:number];
[phoneNums addObject:phone];
}
obj.phoneNumbers = [NSArray arrayWithArray:phoneNums];
setModel.contact = (CNContact*)obj;
[subArray insertObject:setModel atIndex:0];
[self.repeatContactArray addObject:subArray];
}
}
DBB_ContactModel.h 文件
#import <Foundation/Foundation.h>
#import <Contacts/Contacts.h>
NS_ASSUME_NONNULL_BEGIN
@interface DBB_ContactModel : NSObject
@property (nonatomic,assign) BOOL isSelected;
@property (nonatomic,strong) NSString *name;
@property (nonatomic,strong) NSMutableArray *numberArray;
@property (nonatomic,strong) CNContact *contact;
@end