4

我无法理解如何访问 ABAddressBookRef 中地址的属性。我用电话号码做得很好:

ABMultiValueRef phoneNumberProperty = ABRecordCopyValue(person, kABPersonPhoneProperty);
NSArray* phoneNumbers = (NSArray*)ABMultiValueCopyArrayOfAllValues(phoneNumberProperty);
CFRelease(phoneNumberProperty);

但是,唉......我不知道如何为地址做这件事。如果我这样做:

ABMultiValueRef addressProperty = ABRecordCopyValue(person, kABPersonAddressProperty);
NSArray *address = (NSArray *)ABMultiValueCopyArrayOfAllValues(addressProperty);

我找回了看起来像字典的东西,但它是作为数组输入的。如何访问其中的属性?我在网上看到了很多不同的建议,但它们似乎都涉及大约 30 行代码,只是为了从字典中抽出一行!

有人可以帮忙吗?谢谢!

4

1 回答 1

11

对于地址,您会得到一个字典数组,因此您循环遍历数组并从每个字典中提取所需的键值:

ABMultiValueRef addressProperty = ABRecordCopyValue(person, kABPersonAddressProperty);
NSArray *address = (NSArray *)ABMultiValueCopyArrayOfAllValues(addressProperty);
for (NSDictionary *addressDict in address) 
{
    NSString *country = [addressDict objectForKey:@"Country"];
}
CFRelease(addressProperty);

您也可以直接循环,ABMultiValueRef而不是先将其转换为 NSArray:

ABMultiValueRef addressProperty = ABRecordCopyValue(person, kABPersonAddressProperty);

for (CFIndex i = 0; i < ABMultiValueGetCount(addressProperty); i++) 
{
    CFDictionaryRef dict = ABMultiValueCopyValueAtIndex(addressProperty, i);
    NSString *country = (NSString *)CFDictionaryGetValue(dict, kABPersonAddressCountryKey);
    CFRelease(dict);
}

CFRelease(addressProperty);
于 2011-06-27T13:35:20.640 回答