0
我下面的代碼做了嘗試獲得從地址簿中的所有聯繫人的電話號碼:獲取通訊錄中的所有聯繫人電話號碼都崩潰了嗎?
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."];
}
}
然而,它運作良好,在iOS 6中,但不是在iOS的5 它崩潰了在下面的代碼:
ABRecordRef currentPerson =
(__bridge ABRecordRef)[arrayOfPeople objectAtIndex:index];
輸出打印:
*** Terminating app due to uncaught exception 'NSRangeException', reason: '-[__NSCFArray objectAtIndex:]: index (0) beyond bounds (0)'
任何人有意見,爲什麼它得到了崩潰?謝謝!
非常感謝,你救了我的命:')這是一個非常微不足道的問題 – Rendy