首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何从ABPeoplePickerNavigationController获取街道地址

如何从ABPeoplePickerNavigationController获取街道地址
EN

Stack Overflow用户
提问于 2011-12-01 00:04:42
回答 6查看 4.6K关注 0票数 4

我需要联系人的住址。我知道如何获取单值属性,但是街道地址是一个多值属性。Apple的文档展示了如何设置它,但不是检索它。有什么帮助吗?

PS:这不起作用:

代码语言:javascript
运行
复制
ABRecordCopyValue(person, kABPersonAddressStreetKey);
EN

回答 6

Stack Overflow用户

回答已采纳

发布于 2011-12-02 23:36:03

我刚想通了:

代码语言:javascript
运行
复制
ABMultiValueRef st = ABRecordCopyValue(person, kABPersonAddressProperty);
if (ABMultiValueGetCount(st) > 0) {
    CFDictionaryRef dict = ABMultiValueCopyValueAtIndex(st, 0);
    self.street.text = CFDictionaryGetValue(dict, kABPersonAddressStreetKey);
}
票数 9
EN

Stack Overflow用户

发布于 2015-08-29 20:33:34

Swift版本:

代码语言:javascript
运行
复制
    if let addresses : ABMultiValueRef = ABRecordCopyValue(person, kABPersonAddressProperty)?.takeRetainedValue() as ABMultiValueRef? where ABMultiValueGetCount(addresses) > 0 {
        for index in 0..<ABMultiValueGetCount(addresses){
            if let address = ABMultiValueCopyValueAtIndex(addresses, index)?.takeRetainedValue() as? [String:String],
                label = ABMultiValueCopyLabelAtIndex(addresses, index)?.takeRetainedValue()  as? String{
                    print("\(label): \(address) \n")
            }
        }
    }

您可以通过提供对应的key来访问单独的地址字段:

代码语言:javascript
运行
复制
let street  = address[kABPersonAddressStreetKey as String]
let city    = address[kABPersonAddressCityKey as String]
let state   = address[kABPersonAddressStateKey as String]
let zip     = address[kABPersonAddressZIPKey as String]
let country = address[kABPersonAddressCountryKey as String]
let code    = address[kABPersonAddressCountryCodeKey as String]
票数 3
EN

Stack Overflow用户

发布于 2016-10-21 15:23:08

Swift 3.0

代码语言:javascript
运行
复制
//Extract billing address from ABRecord format and assign accordingly
let addressProperty: ABMultiValue = ABRecordCopyValue(billingAddress, kABPersonAddressProperty).takeUnretainedValue() as ABMultiValue

if let dict: NSDictionary = ABMultiValueCopyValueAtIndex(addressProperty, 0).takeUnretainedValue() as? NSDictionary {
     print(dict[String(kABPersonAddressStreetKey)] as? String)
     print(dict[String(kABPersonAddressCityKey)] as? String)
     print(dict[String(kABPersonAddressStateKey)] as? String)
     print(dict[String(kABPersonAddressZIPKey)] as? String)
     print(dict[String(kABPersonAddressCountryKey)] as? String) //"United States"
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8329006

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档