gpt4 book ai didi

objective-c - ABRecordCopyValue() EXC_BAD_ACCESS 错误

转载 作者:可可西里 更新时间:2023-11-01 06:23:39 24 4
gpt4 key购买 nike

在我的应用程序中,我必须检索用户联系人的某些属性。例如,我需要检索联系人的名字、姓氏、中间名、昵称、组织、职务、部门、生日、电子邮件等。我有一些方法来检索这些属性,但只有几个方法有效,尽管他们都非常相似。这是我的代码,用于一种有效的方法(名字)和一种无效的方法(职位):

+(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 是这样定义的类方法:

+(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”的模型类中定义。在我的 Root View Controller 中,我在 viewDidLoad 中放置了一些 NSLog 语句,以查看属性 getter 方法是否正常工作。这是我的测试代码:

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]);
}

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

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] First Name: Jod

2012-06-27 10:37:30.114 Guess Who![80103:f803] Middle Name: Bob

2012-06-27 10:37:30.118 Guess Who![80103:f803] Last Name: Satson

2012-06-27 10:37:30.122 Guess Who![80103:f803] Organization: Johnson and Johnson

2012-06-27 10:37:30.125 Guess Who![80103:f803] Department: NULL

2012-06-27 10:37:30.128 Guess Who![80103:f803] Job Title: NULL


2012-06-27 10:37:30.136 Guess Who![80103:f803] First Name: Shemairan

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] Last Name: Catairan

2012-06-27 10:37:30.184 Guess Who![80103:f803] Organization: Shmairo and Co.

2012-06-27 10:37:30.188 Guess Who![80103:f803] Department: NULL

2012-06-27 10:37:30.193 Guess Who![80103:f803] Job Title: NULL


2012-06-27 10:37:30.202 Guess Who![80103:f803] First Name: Alex

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] Last Name: Corn

2012-06-27 10:37:30.219 Guess Who![80103:f803] Organization: Apple

2012-06-27 10:37:30.225 Guess Who![80103:f803] Department: NULL

2012-06-27 10:37:30.230 Guess Who![80103:f803] Job Title: NULL

在 iOS Simulator Contacts 应用程序中,我确保为每个联系人填写每个字段,但由于某种原因,“部门”和“职位”字段打印不正确。

基本上,我想知道我的“职务”和“部门”获取方法有什么问题。

提前致谢!

最佳答案

虽然在如此小的地址簿样本中似乎不太可能发生这种情况,但这里确实存在内存泄漏,可能会把事情搞砸。

假设您正在使用 arrayOfContacts 来确定 [self contactsCount],您将在这些类方法的每个循环中每次都泄漏 2 个 AddressBook 项目。

在这个小示例中,当您启动 contactsWithJobTitleProperty 时,您只会泄露 48 个地址簿。但是,如果您从一个更大的示例中提取此内容,在该示例中您反复尝试在更大的 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;
}

(顺便说一句,另外一件会导致程序崩溃的事情是,您使用 NSUInteger 作为循环计数器类型,但您测试的值可能是 -1 ...并且由于 0 是 NSUInteger 的底限,如果您的地址簿中没有联系人,这将失败。但这不是发生此崩溃的原因。)

关于objective-c - ABRecordCopyValue() EXC_BAD_ACCESS 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11174892/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com