ABRecordCopyValue()EXC_BAD_ACCESS错误

pas*_*aya 3 objective-c contacts abaddressbook ios

在我的应用程序中,我必须检索用户联系人的某些属性.例如,我需要检索联系人的名字,姓氏,中间名,昵称,组织,职称,部门,生日,电子邮件等.我有一些方法来检索这些属性,只有几个工作,即使他们都非常相似.这是我的一个方法的代码(名字)和一个不是(名称):

+(NSString *)fetchFirstnameForPersonID: (NSUInteger)identifier{

    NSString *firstName;
    ABRecordRef currentPerson = (__bridge ABRecordRef)[[PSAddressBook arrayOfContacts] objectAtIndex:identifier];

    //If the first name property exists
    if ((__bridge_transfer NSString *)ABRecordCopyValue(currentPerson, kABPersonFirstNameProperty) != NULL){

        firstName = (__bridge_transfer NSString *)ABRecordCopyValue(currentPerson, kABPersonFirstNameProperty);        
    }    
    //If the first name property does not exist
    else{
        firstName = @"NULL";
    }
    return firstName;    
}

+(NSString *)fetchJobTitleForPersonID: (NSUInteger)identifier{

    NSString *jobTitle;
    ABRecordRef currentPerson = (__bridge ABRecordRef)[[PSAddressBook arrayOfContacts] objectAtIndex:identifier];

    if ((__bridge_transfer NSString *)ABRecordCopyValue(currentPerson, kABPersonJobTitleProperty) != NULL){

        jobTitle = (__bridge_transfer NSString *)ABRecordCopyValue(currentPerson, kABPersonJobTitleProperty);        
    }    
    else{

        jobTitle = @"NULL";
    }
    return jobTitle;    
}
Run Code Online (Sandbox Code Playgroud)

arrayOfContacts 是一个像这样定义的类方法:

+(NSArray *)arrayOfContacts{

    //Creates an ABAddressBookRef instance containing the data from the address book database
    ABAddressBookRef addressBook = ABAddressBookCreate();

    //Creates an NSArray from the CFArrayRef using toll-free bridging
    NSArray *arrayOfPeople = (__bridge_transfer NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);

    CFRelease(addressBook);

    return arrayOfPeople;
}
Run Code Online (Sandbox Code Playgroud)

这些方法在名为"PSPropertyFetcher"的模型类中定义.在我的根视图控制器中,我在viewDidLoad中放置了一些NSLog语句,以查看属性提取器方法是否正常工作.这是我的测试代码:

NSLog(@"Property Fetchers Test\n");

for(NSUInteger i = 0; i <= ([PSAddressBook contactsCount]-1); i++){

    NSLog(@"First Name: %@", [PSPropertyFetcher fetchFirstnameForPersonID:i]);
    NSLog(@"Middle Name: %@", [PSPropertyFetcher fetchMiddlenameForPersonID:i]);
    NSLog(@"Last Name: %@", [PSPropertyFetcher fetchLastnameForPersonID:i]);
    NSLog(@"Organization: %@", [PSPropertyFetcher fetchOrganizationForPersonID:i]);
    NSLog(@"Department: %@", [PSPropertyFetcher fetchDepartmentForPersonID:i]);        
    NSLog(@"Job Title: %@\n\n", [PSPropertyFetcher fetchJobTitleForPersonID:i]);        
}
Run Code Online (Sandbox Code Playgroud)

这部分起作用; 这是输出:

2012-06-27 10:37:30.094猜猜是谁![80103:f803]物业Fetchers测试

2012-06-27 10:37:30.108猜猜谁![80103:f803]名字:Jod

2012-06-27 10:37:30.114猜猜谁![80103:f803]中间名:鲍勃

2012-06-27 10:37:30.118猜猜谁![80103:f803]姓:Satson

2012-06-27 10:37:30.122猜猜谁![80103:f803]组织:强生公司

2012-06-27 10:37:30.125猜猜谁![80103:f803]系:NULL

2012-06-27 10:37:30.128猜猜谁![80103:f803]职位名称:NULL


2012-06-27 10:37:30.136猜猜谁![80103:f803]名字:Shemairan

2012-06-27 10:37:30.166猜猜谁![80103:f803]中间名:戴特兰

2012-06-27 10:37:30.179猜猜谁![80103:f803]姓:Catairan

2012-06-27 10:37:30.184猜猜是谁[80103:f803]组织:Shmairo and Co.

2012-06-27 10:37:30.188猜猜谁![80103:f803]系:NULL

2012-06-27 10:37:30.193猜猜谁![80103:f803]职位名称:NULL


2012-06-27 10:37:30.202猜猜谁![80103:f803]名字:亚历克斯

2012-06-27 10:37:30.207猜猜谁![80103:f803]中间名:约翰

2012-06-27 10:37:30.213猜猜谁![80103:f803]姓:玉米

2012-06-27 10:37:30.219猜猜谁![80103:f803]组织:Apple

2012-06-27 10:37:30.225猜猜谁![80103:f803]系:NULL

2012-06-27 10:37:30.230猜猜谁![80103:f803]职位名称:NULL

在iOS模拟器联系人应用程序中,我确保为每个联系人填写每个字段,但由于某种原因,"部门"和"职务"字段无法正确打印.

基本上,我想知道我的"职称"和"部门"提取方法有什么问题.

提前致谢!

joh*_*doe 6

虽然在地址簿的这么小的样本中似乎不太可能,但是你确实在这里发生了内存泄漏,这可能会让事情变得更糟.

假设您正在使用arrayOfContacts确定[self contactsCount],那么每次通过这些类方法中的每个循环时,您将泄漏2个AddressBook项.

在这个小样本中,当你启动contactsWithJobTitleProperty时,你只会泄露48个AddressBooks.但是如果你从一个更大的例子中提取这个,你会反复尝试在更大的AddressBook上确定这些值,那么你可能会有数百个悬空的AddressBook对象.

添加CFRelease,如下面的代码片段,看看它是否有帮助.

+(NSArray *)arrayOfContacts{

    //Creates an ABAddressBookRef instance containing the data from the address book database
    ABAddressBookRef addressBook = ABAddressBookCreate();

    //Creates an NSArray from the CFArrayRef using toll-free bridging
    NSArray *arrayOfPeople = (__bridge_transfer NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);
    CFRelease(addressBook);

    return arrayOfPeople;
}
Run Code Online (Sandbox Code Playgroud)

(fwiw,一个会使你的程序崩溃的单独的事情是你使用NSUInteger作为你的循环计数器类型,但你测试的值可能是-1 ......并且因为0是NSUInteger的底线,如果你的地址书中没有联系人,这会失败.但这不是发生这种崩溃的原因.)