如何从 ABAddressBookRef 中提取国家/地区字段?

发布于 2024-11-17 11:35:19 字数 636 浏览 4 评论 0原文

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

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

但是唉......我不知道如何对地址进行此​​操作。如果我这样做:

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

我会返回看起来像字典的内容,但它的类型是数组。我如何访问其中的属性?我在网上看到了很多不同的建议,但它们似乎都涉及大约 30 行代码,只是为了从字典中提取一行!

有人可以帮忙吗?谢谢!

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*)ABMultiValueCopyArrayOfAllValues(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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

抽个烟儿 2024-11-24 11:35:19

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

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

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文