• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

ios - 索引 0 超出空数组的范围' *** 第一次抛出调用堆栈 :

[复制链接]
菜鸟教程小白 发表于 2022-12-13 04:58:55 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我收到如下错误,请帮助我提前谢谢,这是我让空数组的索引 0 超出范围的错误 *** 首先抛出调用堆栈:--->> 以下是我的代码:

-(void)retriveContactsFromAddressBook
{
    //CFErrorRef error = NULL;
     ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);

    if (addressBook!=nil)
    {
        NSArray *allContacts=(__bridge_transfer NSArray*)ABAddressBookCopyArrayOfAllPeople(addressBook);
        NSUInteger i=0;
        for ( i = 0; i<[allContacts count]; i++)
        {
            ABRecordRef contactPerson = (__bridge ABRecordRef)allContacts[i];
             NSString *firstName = (__bridge_transfer NSString*)ABRecordCopyValue(contactPerson, kABPersonFirstNameProperty);
            NSString *lastName=(__bridge_transfer NSString*)ABRecordCopyValue(contactPerson, kABPersonLastNameProperty);

            if (i==0) {
                self.FirstName=[NSMutableArray arrayWithObject:firstName];
                if (lastName==nil) {
                    self.LastName=[[NSMutableArray alloc]init];
                } else {
                    self.LastName=[NSMutableArray arrayWithObject:lastName];
                }

            } else {
                [self.FirstName addObject:firstName];

                if (lastName==nil) {
                    [self.LastName addObject""];
                } else {
                     [self.LastName addObject:lastName];
                }
            }
            NSString *full=[NSString stringWithFormat"%@ %@",[self.FirstName objectAtIndex:i],[self.LastName objectAtIndex:i]];

            if (i==0) {
                self.fullName=[NSMutableArray arrayWithObject:full];
            } else {
                [self.fullName addObject:full];
            }
        }
    }
    else
    {
        NSLog(@"Error Address book empty");
    }
}

我收到如下错误---->>>

>Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array'
    *** First throw call stack:
    (0x2ce265f7 0x3a560c77 0x2cd3a157 0x126419 0x125edb 0x125cb7 0x302ce46d 0x30378325 0x3037824d 0x303777d1 0x3037750b 0x30377271 0x30377209 0x302cb97f 0x2fcf6f65 0x2fcf2951 0x2fcf27d9 0x2fcf21c7 0x2fcf1fd1 0x2fcebedd 0x2cded015 0x2cdea6f9 0x2cdeaafb 0x2cd37b31 0x2cd37943 0x340f0051 0x3032d6f1 0x1e1ea9 0x3aafcaaf)
    libc++abi.dylib: terminating with uncaught exception of type NSException
    (lldb) 



Best Answer-推荐答案


-(void)retriveContactsFromAddressBook
{
    //CFErrorRef error = NULL;
    ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);

    self.LastName=[[NSMutableArray alloc]init];
    self.FirstName=[[NSMutableArray alloc]init];
    self.fullName=[NSMutableArray arrayWithObject:full];

    if (addressBook!=nil)
    {
        NSArray *allContacts=(__bridge_transfer NSArray*)ABAddressBookCopyArrayOfAllPeople(addressBook);
        NSUInteger i=0;
        for ( i = 0; i<[allContacts count]; i++)
        {
            ABRecordRef contactPerson = (__bridge ABRecordRef)allContacts[i];
            NSString *firstName = (__bridge_transfer NSString*)ABRecordCopyValue(contactPerson, kABPersonFirstNameProperty);
            NSString *lastName=(__bridge_transfer NSString*)ABRecordCopyValue(contactPerson, kABPersonLastNameProperty);

            if (firstName == nil) {
                [self.FirstName addObject""];
            } else {
                [self.FirstName addObject:firstName];
            }

            if (lastName==nil) {
                [self.LastName addObject""];
            } else {
                [self.LastName addObject:lastName];
            }

            NSString *full=[NSString stringWithFormat"%@ %@",[self.FirstName objectAtIndex:i],[self.LastName objectAtIndex:i]];

            [self.fullName addObject:full];
        }
    }
    else
    {
        NSLog(@"Error Address book empty");
    }
}

这样使用..

self.LastName=[[NSMutableArray alloc]init];

这一行是问题.. 如果 lastname 为 nil 你没有插入任何对象

NSString *full=[NSString stringWithFormat"%@ %@",[self.FirstName objectAtIndex:i],[self.LastName objectAtIndex:i]];

但是在这一行 [self.LastName objectAtIndex:i] 您尝试从有问题的空数组中检索对象...

关于ios - 索引 0 超出空数组的范围' *** 第一次抛出调用堆栈 :,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28044625/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap