Ios 从通讯簿中获取所有联系人的电话号码发生故障?

Ios 从通讯簿中获取所有联系人的电话号码发生故障?,ios,Ios,我已完成以下代码,尝试从通讯簿获取所有联系人的电话号码: ABAddressBookRef addressBook = ABAddressBookCreate(); NSArray *arrayOfPeople = (__bridge_transfer NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook); NSUInteger index = 0; allContactsPhoneNumber = [[N

我已完成以下代码,尝试从通讯簿获取所有联系人的电话号码:

  ABAddressBookRef addressBook = ABAddressBookCreate();
  NSArray *arrayOfPeople = 
  (__bridge_transfer NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);    
  NSUInteger index = 0;
  allContactsPhoneNumber = [[NSMutableArray alloc] init];

  for(index = 0; index<=([arrayOfPeople count]-1); index++){

    ABRecordRef currentPerson = 
    (__bridge ABRecordRef)[arrayOfPeople objectAtIndex:index];

    NSArray *phones = 
    (__bridge NSArray *)ABMultiValueCopyArrayOfAllValues(
    ABRecordCopyValue(currentPerson, kABPersonPhoneProperty));

    // Make sure that the selected contact has one phone at least filled in.
    if ([phones count] > 0) {
      // We'll use the first phone number only here.
      // In a real app, it's up to you to play around with the returned values and pick the necessary value.
      [allContactsPhoneNumber addObject:[phones objectAtIndex:0]];
    }
    else{
      [allContactsPhoneNumber addObject:@"No phone number was set."];
    }
  }
输出打印:

*** Terminating app due to uncaught exception 'NSRangeException', reason: '-[__NSCFArray objectAtIndex:]: index (0) beyond bounds (0)'

有人知道它为什么会崩溃吗?谢谢

这不是取决于iOS5/iOS6的问题,而是不同测试环境的问题。在一种情况下(我猜是一个模拟器),你的通讯录中有联系人,而在另一种情况下你没有

但是当
[arrayOfPeople count]
为零时,您在
for
循环中的测试将失败,因为
count
返回一个
整数,该整数是无符号的,将
-1
减去
0UL
会产生下溢(解释为无符号整数的
-1
会给出整数的最大值,因为
-1
是负数,无符号整数当然只能存储正整数)

因此,当您没有任何联系人且
[arrayOfPeople count]
为零时,您将进入
for
循环,因此在您的空人员数组中尝试获取索引为0的对象时会崩溃


for
循环中从

for(index = 0; index<=([arrayOfPeople count]-1); index++)

for(index=0;index非常感谢,你救了我的命:')这是一个非常琐碎的问题
for(index = 0; index<=([arrayOfPeople count]-1); index++)
for(index = 0; index<[arrayOfPeople count]; index++)