在iOS应用程序中实现通讯簿,如何删除重复联系人?

在iOS应用程序中实现通讯簿,如何删除重复联系人?,ios,objective-c,nsmutabledictionary,Ios,Objective C,Nsmutabledictionary,我有一个方法-(void)peoplePickerNavigationController:(ABPeoplePickerNavigationController*)peoplePicker didSelectPerson:(ABRecordRef)person 当我点击联系人的名字时,它会被添加到屏幕上的一个表中,但是如果我再次点击相同的名字,就会添加一个重复的值。我有另一种方法,应该显示联系人表。以下是我的全部方法: -(void)peoplePickerNavigationControll

我有一个方法-(void)peoplePickerNavigationController:(ABPeoplePickerNavigationController*)peoplePicker didSelectPerson:(ABRecordRef)person 当我点击联系人的名字时,它会被添加到屏幕上的一个表中,但是如果我再次点击相同的名字,就会添加一个重复的值。我有另一种方法,应该显示联系人表。以下是我的全部方法:

-(void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker didSelectPerson:(ABRecordRef)person {
NSMutableDictionary *contactInfoDict = [[NSMutableDictionary alloc]
                                        initWithObjects:@[@"", @"", @""]
                                        forKeys:@[@"firstName", @"lastName", @"mobileNumber"]];
CFTypeRef generalCFObject;
generalCFObject = ABRecordCopyValue(person, kABPersonFirstNameProperty);
if (generalCFObject) {
    printf("here");
    [contactInfoDict setObject:(__bridge NSString *)generalCFObject forKey:@"firstName"];
    CFRelease(generalCFObject);
}
generalCFObject = ABRecordCopyValue(person, kABPersonLastNameProperty);
if (generalCFObject) {
    [contactInfoDict setObject:(__bridge NSString *)generalCFObject forKey:@"lastName"];
    CFRelease(generalCFObject);
}
ABMultiValueRef phonesRef = ABRecordCopyValue(person, kABPersonPhoneProperty);

for (int i=0; i < ABMultiValueGetCount(phonesRef); i++) {
    CFStringRef currentPhoneLabel = ABMultiValueCopyLabelAtIndex(phonesRef, i);
    CFStringRef currentPhoneValue = ABMultiValueCopyValueAtIndex(phonesRef, i);

    if (CFStringCompare(currentPhoneLabel, kABPersonPhoneMobileLabel, 0) == kCFCompareEqualTo) {
        [contactInfoDict setObject:(__bridge NSString *)currentPhoneValue forKey:@"mobileNumber"];
    }

    CFRelease(currentPhoneLabel);
    CFRelease(currentPhoneValue);
}
CFRelease(phonesRef);


if (_tableData == nil) {
    _tableData = [[NSMutableArray alloc] init];
    printf("there");
}
[_tableData addObject:contactInfoDict];
[self.tableView reloadData];
NSLog(@"value of number is%@",[contactInfoDict objectForKey:@"mobileNumber"]);
[_picker dismissViewControllerAnimated:YES completion:nil];
}



    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"Cell"];
    }

    NSDictionary *contact = _tableData[indexPath.row];
    cell.textLabel.text = [contact[@"firstName"] stringByAppendingFormat:@" %@",contact[@"lastName"]];
    cell.detailTextLabel.text = contact[@"mobileNumber"];


    return cell;
}
-(void)peoplePickerNavigationController:(ABPeoplePickerNavigationController*)peoplePickerDIDSelectPerson:(ABRecordRef)person{
NSMutableDictionary*contactInfoDict=[[NSMutableDictionary alloc]
initWithObjects:@[@”“@”“@”“]
福克斯:@[@“firstName”、@“lastName”、@“mobileNumber”];
CFTypeRef generalCFObject;
generalCFObject=ABRecordCopyValue(person、KabbersonfirstNameProperty);
if(通用对象){
printf(“此处”);
[contactInfoDict setObject:(uu bridge NSString*)GeneralCfoObject forKey:@“firstName”];
CFRelease(generalCFObject);
}
generalCFObject=ABRecordCopyValue(person、kABPersonLastNameProperty);
if(通用对象){
[contactInfoDict setObject:(uu bridge NSString*)GeneralCfoObject forKey:@“lastName”];
CFRelease(generalCFObject);
}
ABMultiValueRef phonesRef=ABRecordCopyValue(person,kABPersonPhoneProperty);
for(int i=0;i
您如何确定两个联系人是否相等(取决于答案是否有用)?你怎么能用它来检查你是否应该向你的
\u tableData
数组中添加一个新联系人,或者该联系人是否已经出现在该集合中?@Jonah因此,也许可以将一个新集合初始化为全局联系人?变量或peoplepickernavigationcontroller方法中的变量,当选择联系人时,将其添加到集合中,当我选择另一个联系人时,检查其是否在集合中..?我认为不需要新变量。您已经有一个集合(
\u tableData
),希望避免向其中添加重复项。那么,您如何确定给定的
contactInfoDict
是否已经在该集合中?也许可以检查一下该dict是否已经在该行前面的tabledata中:[[u tabledata addObject:contactInfoDict]?因为tableData是一个NSMutableArray,所以我会使用[myArray containsObject:myObject];查看contactInfoDict是否已在阵列中,对吗?如果没有,则添加。如果是,什么也不要做!