开发者

Unexpected crash while using looking phone numbers in iPhone address book

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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜