如何保存一个名称字符串和两个不同长度的电子邮件数组和 phone 否

how to save one name string and two different length array of email and phone no

我想这是一个非常简单和基本的问题。

我正在使用 #import <AddressBook/AddressBook.h> 获取设备的联系信息,并且我已经成功实现了相同的功能。

当我从每个联系人收到一封 phone 否和一封电子邮件时,一切正常。但是当我收到多个 phone 否和多个电子邮件时,我的应用程序崩溃了。我想我没有使用正确的保存方法。

所以我真的很想知道如何保存一个名称字符串和一组电子邮件(不同长度)和phone没有(不同长度)作为组一个联系人和所有其他人一样。这样稍后在详细信息屏幕上重现结果就不难了

 CFErrorRef *error = NULL;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error);
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
CFIndex numberOfPeople = ABAddressBookGetPersonCount(addressBook);

for(int i = 0; i < numberOfPeople; i++) {

    ABRecordRef person = CFArrayGetValueAtIndex( allPeople, i );

    NSString *firstName = (__bridge NSString *)(ABRecordCopyValue(person, kABPersonFirstNameProperty));
    NSString *lastName = (__bridge NSString *)(ABRecordCopyValue(person, kABPersonLastNameProperty));
    [values1 addObject:[NSString stringWithFormat:@"%@ %@",firstName,lastName]]; //values1 array to save name of contact

    ABMultiValueRef emails = ABRecordCopyValue(person, kABPersonEmailProperty);
    for (CFIndex j=0; j < ABMultiValueGetCount(emails); j++) {
        NSString* email = (__bridge NSString*)ABMultiValueCopyValueAtIndex(emails, j);
        NSLog(@"Email:%@", email);
        [values2 addObject:[NSString stringWithFormat:@"%@",email]];
    } //values2 array to save email associated to that contact

    ABMultiValueRef phoneNumbers = ABRecordCopyValue(person, kABPersonPhoneProperty);

    for (CFIndex i = 0; i < ABMultiValueGetCount(phoneNumbers); i++) {
        NSString *phoneNumber = (__bridge_transfer NSString *) ABMultiValueCopyValueAtIndex(phoneNumbers, i);
        NSLog(@"phone:%@", phoneNumber);
        [values3 addObject:[NSString stringWithFormat:@"%@",phoneNumber]];
    }//values3 array to save different phone no's associated to that contacts


    NSDictionary *dict = [[NSDictionary alloc]init];

       }

AS 现在我有三个数组,详细信息包含一个联系人的详细信息。现在如何将这三个数组值合并为一个,以便轻松保存和获取多个或数百个联系人的数据

ABAddressBookRef allPeople = ABAddressBookCreate();
CFArrayRef allContacts = ABAddressBookCopyArrayOfAllPeople(allPeople);
CFIndex numberOfContacts  = ABAddressBookGetPersonCount(allPeople);

NSLog(@"numberOfContacts------------------------------------%ld",numberOfContacts);


for(int i = 0; i < numberOfContacts; i++){
    NSString* name = @"";
    NSString* phone = @"";
    NSString* email = @"";

    ABRecordRef aPerson = CFArrayGetValueAtIndex(allContacts, i);
    ABMultiValueRef fnameProperty = ABRecordCopyValue(aPerson, kABPersonFirstNameProperty);
    ABMultiValueRef lnameProperty = ABRecordCopyValue(aPerson, kABPersonLastNameProperty);

    ABMultiValueRef phoneProperty = ABRecordCopyValue(aPerson, kABPersonPhoneProperty);\
    ABMultiValueRef emailProperty = ABRecordCopyValue(aPerson, kABPersonEmailProperty);

    NSArray *emailArray = (__bridge NSArray *)ABMultiValueCopyArrayOfAllValues(emailProperty);
    NSArray *phoneArray = (__bridge NSArray *)ABMultiValueCopyArrayOfAllValues(phoneProperty);


    if (fnameProperty != nil) {
        name = [NSString stringWithFormat:@"%@", fnameProperty];
    }
    if (lnameProperty != nil) {
        name = [name stringByAppendingString:[NSString stringWithFormat:@" %@", lnameProperty]];
    }

    if ([phoneArray count] > 0) {
        if ([phoneArray count] > 1) {
            for (int i = 0; i < [phoneArray count]; i++) {
                phone = [phone stringByAppendingString:[NSString stringWithFormat:@"%@\n", [phoneArray objectAtIndex:i]]];
            }
        }else {
            phone = [NSString stringWithFormat:@"%@", [phoneArray objectAtIndex:0]];
        }
    }

    if ([emailArray count] > 0) {
        if ([emailArray count] > 1) {
            for (int i = 0; i < [emailArray count]; i++) {
                email = [email stringByAppendingString:[NSString stringWithFormat:@"%@\n", [emailArray objectAtIndex:i]]];
            }
        }else {
            email = [NSString stringWithFormat:@"%@", [emailArray objectAtIndex:0]];
        }
    }

    NSLog(@"NAME : %@",name);
    NSLog(@"PHONE: %@",phone);
    NSLog(@"EMAIL: %@",email);
    NSLog(@"\n");
}

如果联系人有,您可以在日志中看到多个联系人和电子邮件。