将联系人添加到组时,应用程序崩溃

时间:2013-03-20 09:28:11

标签: iphone objective-c addressbook

我有一系列联系人,当我尝试将联系人添加到组中时,它会崩溃我的应用程序。这是我的代码:

for (int i = 0; i < [contactArray count]; i++)
{
    ABRecordRef newPerson =  [contactArray objectAtIndex:i];
    ABRecordSetValue(newPerson, kABPersonFirstNameProperty,name, &error);
    ABRecordSetValue(newPerson, kABPersonPhoneProperty, phone,nil);
    ABAddressBookAddRecord(iPhoneAddressBook, newPerson, &error);
    ABAddressBookSave(iPhoneAddressBook, &error);

    BOOL add = ABGroupAddMember(newGroup, newPerson, &error);
    if (add) {
        NSLog(@"added");
    }
}

我的应用程序在ABRecordRef newPerson = [contactArray objectAtIndex:i];

上崩溃了

如何从数组中获取某个人的记录以将其添加到组中?

2 个答案:

答案 0 :(得分:0)

使用:

ABRecordRef newPerson = (id)[contactArray objectAtIndex:i];

ABRecordRef typedefCFTypeRef的{​​{1}},然后转为const void *

答案 1 :(得分:0)

使用以下代码并尝试:

CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBookRef);

    CFIndex nPeople = ABAddressBookGetPersonCount(addressBookRef);
for (int i=0;i < nPeople;i++) {
 ABRecordRef ref = CFArrayGetValueAtIndex(allPeople,i);


        NSString *firstName, *lastName;
        ABMultiValueRef phones;
        firstName =(__bridge NSString *)(ABRecordCopyValue(ref, kABPersonFirstNameProperty));
        lastName  = (__bridge NSString *)(ABRecordCopyValue(ref, kABPersonLastNameProperty));
        phones = (ABMultiValueRef)ABRecordCopyValue(ref, kABPersonPhoneProperty);
 }
    CFRelease(allPeople);