ABRecordCopyValue()EXC_BAD_ACCESS错误 [英] ABRecordCopyValue() EXC_BAD_ACCESS Error
问题描述
在我的应用程序中,我必须检索用户联系人的某些属性。例如,我需要检索联系人的名字,姓氏,中间名,昵称,组织,职务,部门,生日,电子邮件等。我有一些方法可以检索这些属性,即使只有几个工作他们都很相似。这是我的一种有效的方法代码(名字),一种无效的方法(职位):
In my app, I have to retrieve certain properties of the users contacts. For example, I need to retrieve a contact's first name, last name, middle name, nickname, organization, job title, department, birthday, email, etc. I have some methods to retrieve these properties, and only a couple work, even though they all are very similar. Here is my code for one method that is working (first name) and one that is not (job title):
+(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;
}
arrayOfContacts
是一个类方法,定义如下
arrayOfContacts
is a class method defined like this:
+(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;
}
这些方法在称为 PSPropertyFetcher的模型类中定义。在我的根视图控制器中,我将一些NSLog语句放入viewDidLoad中,以查看属性访存方法是否正常运行。这是我用于测试的代码:
These methods are defined in a model class called "PSPropertyFetcher". In my root view controller, I put some NSLog statements in viewDidLoad to see if the property fetcher methods are working properly. Here is my code for the tests:
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]);
}
这仅部分起作用;这是输出:
This works only partly; this is the output:
2012-06-27 10:37:30.094猜猜谁![80103:f803]属性提取程序测试
2012-06-27 10:37:30.094 Guess Who![80103:f803] Property Fetchers Test
2012-06-27 10:37:30.108 Guess Who![80103:f803]名字:Jod
2012-06-27 10:37:30.108 Guess Who![80103:f803] First Name: Jod
2012-06-27 10:37:30.114 Guess Who ![80103:f803]中间名:Bob
2012-06-27 10:37:30.114 Guess Who![80103:f803] Middle Name: Bob
2012-06-27 10:37:30.118猜猜谁![80103:f803]姓氏:Satson
2012-06-27 10:37:30.118 Guess Who![80103:f803] Last Name: Satson
2012-06-27 10:37:30.122猜猜谁![80103:f803]组织:约翰逊和约翰逊
2012-06-27 10:37:30.122 Guess Who![80103:f803] Organization: Johnson and Johnson
2012-06-27 10:37:30.125猜猜谁![80103:f803]部门:NULL
2012-06-27 10:37:30.125 Guess Who![80103:f803] Department: NULL
2012-06-27 10:37:30.128猜猜谁![80103:f803]工作标题:NULL
2012-06-27 10:37:30.128 Guess Who![80103:f803] Job Title: NULL
2012-06-27 10:37:30.136猜猜是谁![80103:f803]名字:Shemairan
2012-06-27 10:37:30.136 Guess Who![80103:f803] First Name: Shemairan
2012-06-27 10:37:30.166猜猜谁![80103:f803]中间名:Daitran
2012-06-27 10:37:30.166 Guess Who![80103:f803] Middle Name: Daitran
2012-06-27 10:37 :30.179 Guess Who![80103:f803]姓:Catairan
2012-06-27 10:37:30.179 Guess Who![80103:f803] Last Name: Catairan
2012-06-27 10:37:30.184 Guess Who![80103:f803]组织:Shmairo and Co.
2012-06-27 10:37:30.184 Guess Who![80103:f803] Organization: Shmairo and Co.
2012-06-27 10:37:3 0.188猜猜谁![80103:f803]部门:空
2012-06-27 10:37:30.188 Guess Who![80103:f803] Department: NULL
2012-06-27 10:37:30.193猜猜谁![80103:f803]职位:空
2012-06-27 10:37:30.193 Guess Who![80103:f803] Job Title: NULL
2012-06-27 10:37:30.202猜猜谁![80103:f803]名字:亚历克斯
2012-06-27 10:37:30.202 Guess Who![80103:f803] First Name: Alex
2012-06-27 10:37:30.207猜猜谁![80103:f803]中间名:约翰
2012-06-27 10:37:30.207 Guess Who![80103:f803] Middle Name: John
2012-06-27 10:37:30.213 Guess Who![80103:f803]姓氏:Corn
2012-06-27 10:37:30.213 Guess Who![80103:f803] Last Name: Corn
2012-06-27 10:37:30.219 Guess Who![80103:f803]组织:Apple
2012-06-27 10:37:30.219 Guess Who![80103:f803] Organization: Apple
2012-06-27 10:37:30.225猜猜谁![80103:f803]部门:NULL
2012-06-27 10:37:30.225 Guess Who![80103:f803] Department: NULL
2012-06-27 10:37 :30.230 Guess Who![80103:f803]职务:NULL
2012-06-27 10:37:30.230 Guess Who![80103:f803] Job Title: NULL
在iOS Simulator Contacts应用程序中,我确保为每个字段填写每个字段联系,但由于某些原因,部门和职位
In the iOS Simulator Contacts application, I made sure to fill out every field for each contact, but for some reason, the "Department" and "Job Title" fields are not printing correctly.
基本上,我想知道我的职位名称出了什么问题?和部门
Basically, I'm wondering what's wrong with my "Job Title" and "Department" fetcher methods.
预先感谢!
推荐答案
虽然不太可能在这么小的地址簿样本中,您的确存在内存泄漏,这可能使事情崩溃。
While it doesn't seem likely on such a small sampling of an address book, you do have a memory leak here that could be blowing things out.
假设您使用的是 arrayOfContacts
来确定 [self contactCount]
,您每次通过这些类方法中的每个循环都会泄漏2个AddressBook项目。
Assuming you are using arrayOfContacts
to determine [self contactsCount]
, you will be leaking 2 AddressBook items every time through every loop in those class methods.
在这个小示例中,当您启动contactsWithJobTitleProperty时,您只会泄漏48个地址簿。但是,如果您从一个较大的示例中反复尝试在更大的AddressBook上确定这些值,则可能会挂起数百个悬挂的AddressBook对象。
In this small sample, you will have only leaked 48 AddressBooks by the time you start contactsWithJobTitleProperty. But if you've pulled this from a larger example where you're repeatedly attempting to determine these values on a much larger AddressBook, you could have hundreds of dangling AddressBook objects hanging around.
按照以下代码片段添加CFRelease,看看是否有帮助。
Add CFRelease as in the following snippet, and see if it helps.
+(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;
}
(fwiw,会使您的程序崩溃的另一件事是您正在使用NSUInteger作为循环计数器类型,但是您使用一个可能为-1的值进行测试,并且由于0是NSUInteger的下限,因此如果您的地址簿中没有联系人,这将失败。但这不是为什么发生这种崩溃。)
(fwiw, a separate thing that will crash your program is that you are using NSUInteger as your loop counter type, but you test against a value that could potentially be -1 ... and since 0 is the floor for NSUInteger, if your address book has no contacts in it, this will fail. but that is not why this crash is occurring.)
这篇关于ABRecordCopyValue()EXC_BAD_ACCESS错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!