2
votes

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).

1

1 Answers

1
votes

I think you should first check the return values of CFArrayGetValueAtIndex() and ABRecordCopyValue() to ensure you have real values, and not NULL.