0

我正在使用这串代码,通过它我正在检查如果一个联系人有 2 个号码,那么他们将显示 2 个同名但号码不同的联系人.. 但这段代码有时无法正确获取数据

self.contacts = [ABContactsHelper personalSorting];
 NSString *emailAre;
   for (int i=0; i< [self.contacts count]; i++)
    {
        ABContact *contObj = [self.contacts objectAtIndex:i];        
        if([contObj.emailDictionaries count] >=1)
            emailAre = [[contObj.emailDictionaries objectAtIndex:0]objectForKey:@"value"];
           // emailAre = @"";
        else
            emailAre = @"";
        for (int j=0; j< [contObj.phoneDictionaries  count]; j++) 
        {
            if([contObj.phoneDictionaries count] != 0)
            {
                if([[contObj.phoneDictionaries objectAtIndex:j] count] >= 1)
                {
                    NSLog(@"***************************************");
                    NSLog(@"contact Name %@",contObj.firstname);
                    NSLog(@"contact Number %@",[[contObj.phoneDictionaries objectAtIndex:j]objectForKey:@"value"]);
                    NSLog(@"***************************************");

                    NSString *phones = [[contObj.phoneDictionaries objectAtIndex:j]objectForKey:@"value"];

                    CFErrorRef  anError = NULL;
                    ABRecordRef aRecord = ABPersonCreate(); 
                    // Set the record values
                    ABRecordSetValue(aRecord, kABPersonFirstNameProperty, 
                                     contObj.firstname, &anError); 

                     ABMutableMultiValueRef multiTelephone = ABMultiValueCreateMutable(kABMultiStringPropertyType);
                     ABMultiValueAddValueAndLabel(multiTelephone, phones, kABPersonPhoneMainLabel, NULL);

                     ABRecordSetValue(aRecord, kABPersonPhoneProperty, multiTelephone, &anError);

                    /// Image
                    NSData *dataRef = UIImagePNGRepresentation(contObj.image);

                    ABPersonSetImageData(aRecord, (CFDataRef)dataRef, &anError);
                    /// Image

                     ABMutableMultiValueRef multiEmail = ABMultiValueCreateMutable(kABMultiStringPropertyType);

                     ABMultiValueAddValueAndLabel(multiEmail, emailAre, kABWorkLabel, NULL);

                     ABRecordSetValue(aRecord, kABPersonEmailProperty, multiEmail, &anError);

                     ABContact *newContract = [ABContact contactWithRecord:aRecord]; 

                    [dup addObject:newContract];
                }

            }

        }
    }
4

1 回答 1

0

按着这些次序:

  1. 以与检索地址簿数据相同的方式检索地址簿数据。然后
  2. 制作一个联系人数组(其中包含姓名字典和联系人号码)
  3. 使用重复的用户名数据在单独的索引中添加每个联系人编号
  4. 现在将此联系人数据填充到tableView
于 2013-02-15T12:08:11.140 回答