开发者

How do I pull the Country field out of an ABAddressBookRef?

I'm having trouble understanding how to access the properties of an address in a an ABAddressBookRef. I've done it okay with telephone numbers:

ABMultiValueRef phoneNumberProperty = ABRecordCopyValue(person, kABPersonPhoneProperty);
NSArray* phoneNumbers = (NSArray*)AB开发者_Go百科MultiValueCopyArrayOfAllValues(phoneNumberProperty);
CFRelease(phoneNumberProperty);

But alas... I can't work out how to do it for addresses. If I do this:

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

I get back what looks like a Dictionary, but it is typed as an Array. How do I access the properties within it? I've seen loads of different suggestions on the web, but they all seem to involve about 30 lines of code, just to pull out one line from a dictionary!

Can anyone help please? Thanks!


For the addresses, you get an array of dictionaries so you loop through the array and extract the key value you want from each dictionary:

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

You can also loop directly through the ABMultiValueRef instead of converting it to an NSArray first:

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);
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜