首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CFArrayGetValueAtIndex返回非ABRecordRef

CFArrayGetValueAtIndex返回非ABRecordRef
EN

Stack Overflow用户
提问于 2011-09-12 08:30:46
回答 2查看 2.6K关注 0票数 1

我一直在某个用户的iPhones上崩溃,我终于找到了一个可以复制这个问题的人。以下是代码的关键部分

代码语言:javascript
复制
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的形式返回。有关于如何检查结果类型的想法吗?我正试图防止这件事使用户的手机崩溃。一旦我走到这一行:

代码语言:javascript
复制
 ABRecordCopyValue(ref, kABPersonFirstNameProperty);

我在这条线路上得到了一个EXC_BAD_ACCESS。当日志文件如下所示时,就会发生这种情况:

代码语言:javascript
复制
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

任何帮助都将不胜感激!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-09-12 17:40:28

我很确定崩溃的发生是因为contactCount设置错误:

all people sortedPeople is set to all people of one source

  • contactCount is set to all people in

(将通讯录中所有人的计数设置为一个通讯录的所有人的计数

因此,如果您的地址簿有多个包含人员的源,则迭代将离开sortedPeople的界限。要修复此问题,请替换

代码语言:javascript
复制
CFIndex contactCount = ABAddressBookGetPersonCount(addressbook);

使用

代码语言:javascript
复制
CFIndex contactCount = CFArrayGetCount(sortedPeople);
票数 7
EN

Stack Overflow用户

发布于 2011-09-12 12:25:04

您可以使用ABRecordGetRecordType(ref) == kABPersonType对地址簿结果进行类型检查,以确保它是ABPerson,而不是ABGroup

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7382293

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档