无法在不崩溃的情况下设置ABPeoplePickerNavigationController的addressBook属性

时间:2020-12-27 21:07:11

I want to display an ABPeoplePicker with only people who have a geographic address defined.

我想显示一个只有定义了地理地址的人的ABPeoplePicker。

So I create an addressBook and remove people that dont have an address:

所以我创建了一个addressBook并删除了没有地址的人:

addressBook = ABAddressBookCreate();
NSArray *peopleList = (NSArray *)ABAddressBookCopyArrayOfAllPeople( addressBook );
NSLog(@"There are %d people in addressBook", ABAddressBookGetPersonCount(addressBook));
for (id peopleRecord in peopleList) {
  ABMultiValueRef mv = ABRecordCopyValue((ABRecordRef)peopleRecord, kABPersonAddressProperty);
  CFIndex numberOfAddresses = ABMultiValueGetCount(mv);
  if( numberOfAddresses == 0 ) {
    CFErrorRef err;
    ABAddressBookRemoveRecord( addressBook, (ABRecordRef)peopleRecord, &err);
  }
}
[peopleList release];
NSLog(@"There are now %d people in addressBook", ABAddressBookGetPersonCount(addressBook));
ABPeoplePickerNavigationController *peoplePicker = [[ABPeoplePickerNavigationController alloc] init];
NSNumber* addressProp = [NSNumber numberWithInt:kABPersonAddressProperty];
[peoplePicker setAddressBook:addressBook];
peoplePicker.displayedProperties = [NSArray arrayWithObject:addressProp];
[peoplePicker setPeoplePickerDelegate:self];
[self presentModalViewController:peoplePicker animated:YES];

For info, before filtering I have 125 records, and after filtering I have 93 records.

有关信息,在过滤之前我有125条记录,过滤后我有93条记录。

When I display the peoplePicker and scroll through it, it crashes with:

当我显示peoplePicker并滚动浏览它时,它会崩溃:

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

Any idea what's wrong?

知道什么是错的吗?

1 个解决方案

#1


The fact that the NSRangeException's value is 49 (rather than some number between 93 and 125), I initially suspect that it's not directly related to this address book. Add a breakpoint for objc_exception_throw. That will cause you to drop into the debugger at the point of the exception, letting you see who's actually throwing it.

NSRangeException的值为49(而不是93到125之间的某个数字)的事实,我最初怀疑它与这个地址簿没有直接关系。为objc_exception_throw添加断点。这将导致您在异常点进入调试器,让您看到谁实际抛出它。

#1


The fact that the NSRangeException's value is 49 (rather than some number between 93 and 125), I initially suspect that it's not directly related to this address book. Add a breakpoint for objc_exception_throw. That will cause you to drop into the debugger at the point of the exception, letting you see who's actually throwing it.

NSRangeException的值为49(而不是93到125之间的某个数字)的事实,我最初怀疑它与这个地址簿没有直接关系。为objc_exception_throw添加断点。这将导致您在异常点进入调试器,让您看到谁实际抛出它。