Iphone KabPersonalAddressProperty在ABAddressBookGetPersonWithRecordID下导致崩溃

Iphone KabPersonalAddressProperty在ABAddressBookGetPersonWithRecordID下导致崩溃,iphone,addressbook,Iphone,Addressbook,我试图成为一个ABRecordRef,代表一个人的联系方式从通讯录。我构建了两个函数,它们调用一个函数,用ABRecordRef中的信息填充个人数据结构 下面是三个函数的函数声明: + (NSMutableArray*) getAllContactProfiles{ NSMutableArray *listOfProfile = [[NSMutableArray alloc] init]; //---get the contact information for the ap

我试图成为一个ABRecordRef,代表一个人的联系方式从通讯录。我构建了两个函数,它们调用一个函数,用ABRecordRef中的信息填充个人数据结构

下面是三个函数的函数声明:

+ (NSMutableArray*) getAllContactProfiles{

    NSMutableArray *listOfProfile = [[NSMutableArray alloc] init];

    //---get the contact information for the api
    ABAddressBookRef addressBook = ABAddressBookCreate(); 
    CFArrayRef people = ABAddressBookCopyArrayOfAllPeople(addressBook);
    CFIndex numberOfPeopleInAddressBook = ABAddressBookGetPersonCount(addressBook);

    //<- Here I loop through all the contacts and pass the ABRecordRef into the following function

    //---release the variables---
    CFRelease(addressBook);
    CFRelease(people);
    [listOfProfile autorelease];

    return listOfProfile;
}
一切都很顺利。但是,当我尝试通过address bookGetPersonWithRecordId获取ABRecordRef时,就像在下一个方法中一样:

下一种方法

+ (MSProfileEntry*) getPersonProfileThroughContactId:(NSInteger*)contactId{
    ABAddressBookRef addressBook = ABAddressBookCreate();
    ABRecordRef person = 
    ABAddressBookGetPersonWithRecordID(addressBook, (ABRecordID)contactId);
    CFRelease(addressBook);
    if (person == nil) {
        return nil;
    }
    return [MSContactUtil getPersonProfileThroughABRecordRef:person];
}
整个应用程序在线崩溃:
ABRecordCopyValue(person,kabbersonfirstnameproperty)

现在的问题是
ABRecordCopyValue(person,kABPersonFirstNameProperty)
abAddressBookCopyArrayFallPeople
配合使用非常好,但会导致应用程序与
ABAddressBookGetPersonWithRecordID
一起崩溃

有人知道如何解决这个问题吗?我真的不想只为了寻找联系人而在整个联系人群中循环。

两件事:

  • 您通过contactId将
    (NSInteger*)contactId
    传递给
    GetPersonProfile
    ,然后调用
    ABAddressBookGetPersonWithRecordID(addressBook,(ABRecordID)contactId)。实际上,您传递的是一个整数的地址,它保存联系人id,而不是id本身
  • 如果(person==nil),则检查
    ,但person可能不是nil-您应该与
    NULL
    进行比较。我相信在你的情况下它是空的(因为我前面的观点)
这两件事一起导致了车祸

只需按原样传递一个整数,而不是它的地址

编辑:
像这样:

+ (MSProfileEntry*)getPersonProfileThroughContactId:(NSInteger)contactId
两件事:

  • 您通过contactId将
    (NSInteger*)contactId
    传递给
    GetPersonProfile
    ,然后调用
    ABAddressBookGetPersonWithRecordID(addressBook,(ABRecordID)contactId)。实际上,您传递的是一个整数的地址,它保存联系人id,而不是id本身
  • 如果(person==nil),则检查
    ,但person可能不是nil-您应该与
    NULL
    进行比较。我相信在你的情况下它是空的(因为我前面的观点)
这两件事一起导致了车祸

只需按原样传递一个整数,而不是它的地址

编辑:
像这样:

+ (MSProfileEntry*)getPersonProfileThroughContactId:(NSInteger)contactId

结果是内存问题。我忘了保留“
通讯录”
”。当我执行以下命令时:

firstName = ABRecordCopyValue(person, kABPersonFirstNameProperty);
通讯簿
”已被清除。在查询“
person
”中的详细信息时,我们仍然需要“
addressBook

所以,记住写在下面的一行,你会安全的


CFRetain(地址簿)

结果是内存问题。我忘了保留“
通讯录”
”。当我执行以下命令时:

firstName = ABRecordCopyValue(person, kABPersonFirstNameProperty);
通讯簿
”已被清除。在查询“
person
”中的详细信息时,我们仍然需要“
addressBook

所以,记住写在下面的一行,你会安全的


CFRetain(地址簿)

谢谢!我要试试看!我已经编辑了我的答案。请按原样查看
getPersonProfileThroughContactId
方法…谢谢!我要试试看!我已经编辑了我的答案。请参阅
getPersonProfileThroughContactId
方法,它应该是。。。