0

我正在尝试从 Cocoa 中的 ABPerson 对象中获取 Country 。

我要做的是:

NSString *country = [person valueForProperty:kABAddressCountryKey];

我在控制台中得到了这个:

-[ABPerson valueForProperty:Country] - 未知属性。对于每个未知属性,每个会话,此警告只会显示一次。

获取人员的组织名称(kABOrganizationProperty)、名字(kABFirstNameProperty)和姓氏(kABLastNameProperty)有效。

有任何想法吗?

4

1 回答 1

0

正如键kABAddressCountryKey所暗示的,它代表一个地址的值。由于人可以有多个地址(表示为字典),因此您必须循环地址以获取国家/地区:

ABMultiValueRef addressesRef = ABRecordCopyValue(personRef, kABPersonAddressProperty);

for (int i = 0; i < ABMultiValueGetCount(addressesRef); i++)
{
    CFDictionaryRef oneAddressRef = ABMultiValueCopyValueAtIndex(addressesRef, i);
    NSString *country = CFRetain(CFDictionaryGetValue(oneAddressRef, kABPersonAddressCountryCodeKey));
    // Do fancy things with country...
    CFRelease(country);
    CFRelease(oneAddressRef);
}

CFRelease(addressesRef);

还没有最终测试它,但它应该可以这样工作。还要考虑Core Foundation Memory Management

于 2013-08-06T13:00:36.937 回答