Unexpected crash while using looking phone numbers in iPhone address book

Posted by FX on Stack Overflow See other posts from Stack Overflow or by FX
Published on 2010-06-03T15:32:21Z Indexed on 2010/06/03 15:34 UTC
Read the original article Hit count: 162

Filed under:

I have a rather unexpected result with the following code:

ABAddressBookRef addressBook = ABAddressBookCreate();
CFArrayRef people = ABAddressBookCopyArrayOfAllPeople(addressBook);
CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);

NSLog (@"npeople = %d = %d", nPeople, CFArrayGetCount(people));
for (CFIndex i = 0; i < nPeople; i++)
{
    ABRecordRef record = CFArrayGetValueAtIndex(people,i);
    ABMultiValueRef numbers = ABRecordCopyValue(record, kABPersonPhoneProperty);
    CFIndex count = ABMultiValueGetCount(numbers);
    NSLog (@"%d %d", i, count);
}

I never reach the second NSLog, it crashes on the call to ABMultiValueGetCount with:

: * -[NSCFString count]: unrecognized selector sent to instance 0x383e72f4 : Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '** -[NSCFString count]: unrecognized selector sent to instance 0x383e72f4'

In short, it's behaving as if my "numbers" variable was an NSString. While I wrote the code above myself, the same can be found in many examples on the web. Does someone here spot anything wrong with it?

Oh, and also: it only crashes in Release mode (in debug mode, all is fine).

© Stack Overflow or respective owner

Related posts about iphone-sdk