無法在不崩潰的情況下設置 ABPeoplePickerNavigationController 的 addressBook 屬性 (Can't set the addressBook property of ABPeoplePickerNavigationController without crashing)


問題描述

無法在不崩潰的情況下設置 ABPeoplePickerNavigationController 的 addressBook 屬性 (Can't set the addressBook property of ABPeoplePickerNavigationController without crashing)

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

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

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.

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

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

Any idea what's wrong?

‑‑‑‑‑

參考解法

方法 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.

(by Stephan BurlotRob Napier)

參考文件

  1. Can't set the addressBook property of ABPeoplePickerNavigationController without crashing (CC BY‑SA 3.0/4.0)

#cocoa-touch #iphone






相關問題

防止 UITableView 在自定義子視圖處理觸摸時滾動 (Prevent UITableView from scrolling when custom subview is handling touches)

NSMutableArray removeObjectAtIndex 在第二次執行時崩潰 (NSMutableArray removeObjectAtIndex crashes on second execution)

Apakah NSURLConnections termasuk dalam model atau pengontrol saya? (Do NSURLConnections belong in my model or controller?)

Lợi ích của việc sử dụng NSString tĩnh cho CellIdentifier là gì? (Whats the advantage of using a static NSString for CellIdentifier?)

CGContextRef 中的坐標係是什麼樣的? (How does the coordinate system in an CGContextRef look like?)

無法在不崩潰的情況下設置 ABPeoplePickerNavigationController 的 addressBook 屬性 (Can't set the addressBook property of ABPeoplePickerNavigationController without crashing)

tableViewHeader 拒絕多個子視圖 (tableViewHeader rejecting multiple sub-views)

界面生成器添加了一個標籤欄按鈕,但未顯示 (Interface builder added one tab bar button, but not showing up)

iVar getter / 方法同名? (iVar getter / method with same name?)

如何更改主視圖是基於導航的應用程序! (how to change main view is navigation based app!)

捕捉返回按鈕導航事件 (Catching back button navigation event)

如何將對象轉發到單獨的子視圖? (How can i forward objects to separate subviews?)







留言討論