我一直在某个用户的iPhones上崩溃,我终于找到了一个可以复制这个问题的人。以下是代码的关键部分
ABAddressBookRef addressbook = ABAddressBookCreate();
if( addressbook )
{
//Got this via http://stackoverflow.com/questions/4641229/code-example-for-abaddressbookcopyarrayofallpeopleinsourcewithsortordering
ABRecordRef source = ABAddressBookCopyDefaultSource(addressbook);
CFArrayRef sortedPeople = ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering(addressbook, source, kABPersonSortByFirstName);
//Sort them first
if( sortedPeople )
{
CFIndex contactCount = ABAddressBookGetPersonCount(addressbook);
for( int i = 0; i<contactCount; i++ )
{
ABRecordRef ref = CFArrayGetValueAtIndex(sortedPeople, i);
NSMutableString *fName = [[[NSMutableString alloc] init] autorelease];
NSMutableString *lName = [[[NSMutableString alloc] init] autorelease];
NSMutableDictionary *identifiers = [[[NSMutableDictionary alloc]init]autorelease];
if( ref )
{
//Get the user's name first
NSLog(@"%@ is the reference", ref);
CFStringRef firstName = ABRecordCopyValue(ref, kABPersonFirstNameProperty);
if( firstName )
{
NSString *fn = [NSString stringWithFormat:@"%@",firstName];
if([fn hasPrefix:@"(null"])
[fName appendString:@""];
else
{
[fName appendString:[NSString stringWithFormat:@"%@", firstName]];
[fName setString:[fName stringByReplacingCharactersInRange:NSMakeRange(0,1) withString:[[fName substringToIndex:1]uppercaseString]]];
}
CFRelease(firstName);
}
}
}
}
}显然,这行代码的结果是: ABRecordRef ref = CFArrayGetValueAtIndex(sortedPeople,i);
有时会以NSCFType而不是ABPerson的形式返回。有关于如何检查结果类型的想法吗?我正试图防止这件事使用户的手机崩溃。一旦我走到这一行:
ABRecordCopyValue(ref, kABPersonFirstNameProperty);我在这条线路上得到了一个EXC_BAD_ACCESS。当日志文件如下所示时,就会发生这种情况:
2011-09-11 17:24:31.355 Holler[1345:707] <CPRecord: 0x6642fb0 ABPerson> is the reference
2011-09-11 17:24:31.358 Holler[1345:707] <CPRecord: 0x66431d0 ABPerson> is the reference
2011-09-11 17:24:31.361 Holler[1345:707] <CPRecord: 0x66433b0 ABPerson> is the reference
2011-09-11 17:24:31.365 Holler[1345:707] <CPRecord: 0x6640fd0 ABPerson> is the reference
2011-09-11 17:24:31.369 Holler[1345:707] <CPRecord: 0x6643510 ABPerson> is the reference
2011-09-11 17:24:31.372 Holler[1345:707] __NSCFType is the reference任何帮助都将不胜感激!
发布于 2011-09-12 17:40:28
我很确定崩溃的发生是因为contactCount设置错误:
all people sortedPeople is set to all people of one source
(将通讯录中所有人的计数设置为一个通讯录的所有人的计数
因此,如果您的地址簿有多个包含人员的源,则迭代将离开sortedPeople的界限。要修复此问题,请替换
CFIndex contactCount = ABAddressBookGetPersonCount(addressbook);使用
CFIndex contactCount = CFArrayGetCount(sortedPeople);发布于 2011-09-12 12:25:04
您可以使用ABRecordGetRecordType(ref) == kABPersonType对地址簿结果进行类型检查,以确保它是ABPerson,而不是ABGroup。
https://stackoverflow.com/questions/7382293
复制相似问题