kABPersonFirstNameProperty ...正在浏览EXC_BAD_ACCESS [英] kABPersonFirstNameProperty... trowing EXC_BAD_ACCESS
问题描述
我正在阅读通讯录中的联系人...一切正常,直到我测试没有 名字(因为我可以只用电子邮件,电话或电话来创建联系人……). 代码(精简)是这样的:
Im reading the address book contacts... everything goes well until I test a contact with no First Name ( Since I can create a contact with just an email or a phone or wathever....). The code (reduced) is this:
- (NSMutableArray *) getContactsInfo {
NSMutableArray *contactsList = [[NSMutableArray alloc] init];
localAddressBook = ABAddressBookCreate();
int contactsLength = (int)ABAddressBookGetPersonCount(localAddressBook);
if (contactsLength < 1)
return nil;
for(int currentContact=1; currentContact < (contactsLength + 1); currentContact++) {
ABRecordRef person = ABAddressBookGetPersonWithRecordID(localAddressBook,(ABRecordID) currentContact);
firstName = (NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
NSLog(@"%@", firstName);
[contactsList addObject:firstName];
CFRelease(person);
}
return contactsList;
}
我得到的输出是这样:
2010-02-15 14:16:25.616 testApp[7065:207] Contact0
2010-02-15 14:16:25.618 testApp[7065:207] Contact1
2010-02-15 14:16:25.619 testApp[7065:207] Contact2
Program received signal: "EXC_BAD_ACCESS".
我有3个联系人的名字和姓氏 为了测试目的,只创建了一个姓氏.
I have 3 contacts with First and Last names And one created with just the last name, for test purposes.
似乎我可以正确读取任何属性,例如电子邮件或带有数组的地址...但是当联系人缺少名字属性"时,应用程序将崩溃.
It seems I can properly read any property such as email or address with arrays... but when a contact lacks First Name Property the app crashes.
推荐答案
您在代码中做错了什么:您假设记录ID是连续的,并且从1开始.不能依靠这个.
You are doing something very wrong in your code: you are assuming that the record IDs are sequential and starting at 1. This is not the case at all, you cannot rely on this.
相反,您应该使用ABAddressBookCopyArrayOfAllPeople
在通讯簿中查找所有记录,然后使用Core Foundation CFArray
函数来获取各个项目.
What you should do instead is use ABAddressBookCopyArrayOfAllPeople
to find all records in the Address Book and then use the Core Foundation CFArray
functions to get to the individual items.
(是的,iPhone中的通讯录API很糟糕)
(Yes, the Address Book API in the iPhone is terrible)
这篇关于kABPersonFirstNameProperty ...正在浏览EXC_BAD_ACCESS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!