Kal 的答案实际上是不准确的 - 即因为“ABMultiValueCopyValueAtIndex”采用索引而不是标识符。
标识符值是静态的(如枚举)
- “家庭电子邮件”始终为“0”
- “工作电子邮件”始终为“1”。
因此,当所选人员仅存储 1 封电子邮件(即“工作电子邮件”)时,它会崩溃。由于标识符是“1”,但我们需要索引“0”。
幸运的是,我们可以使用以下来获取索引:
int index = ABMultiValueGetIndexForIdentifier(emails, identifier);
代码:
if (property == kABPersonEmailProperty) {
ABMultiValueRef emails = ABRecordCopyValue(person, property);
NSString *count = [NSString stringWithFormat:@"Count: %d Identifier: %d", ABMultiValueGetCount(emails), identifier];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"alert" message:count delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
if(ABMultiValueGetCount(emails) > 0)
{
int index = ABMultiValueGetIndexForIdentifier(emails, identifier);
CFStringRef emailTypeSelected = ABMultiValueCopyLabelAtIndex(emails, index);
CFStringRef emailTypeSelectedLocalized = ABAddressBookCopyLocalizedLabel(ABMultiValueCopyLabelAtIndex(emails, index));
CFStringRef emailValueSelected = ABMultiValueCopyValueAtIndex(emails, index);
self.lblEmailType.text = (NSString *) emailTypeSelected;
self.lblEmailTypeLocalized.text = (NSString *) emailTypeSelectedLocalized;
self.lblEmailValue.text = (NSString *) emailValueSelected;
}
[ self dismissModalViewControllerAnimated:YES ];
return NO;
}
return YES;