我一直在一些用戶的iPhone上發生崩潰,我終於找到了一個能夠複製問題的人。下面的代碼CFArrayGetValueAtIndex返回非ABRecordRef
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
任何幫助將非常感謝!
這給出了一個錯誤「不兼容的整數指針轉換,並傳遞‘ABRecordType’(又名‘無符號整型’)至類型的參數‘CFTypeRef’(又名'const const void *') –
即使我轉換爲指針,例如類似於此註釋之後的代碼,它也不起作用int test = ABRecordGetRecordType(ref); int * testp = &test; if(CFEqual(testp, kABPersonType)) –
噢,抱歉,對不起,現在就糾正這個問題,傻了我 – darvids0n