我通过如下代码,从我的Mac上的通讯录,获取某人的国家。
let address = person.value(forProperty: kABAddressProperty) as? ABMultiValue
print(((adresse?.value(at: 0) as? NSMutableDictionary)?.value(forKey: kABAddressCountryKey) as? String) ?? "No Country")
print(((adresse?.value(at: 0) as? NSMutableDictionary)?.value(forKey: kABAddressCountryCodeKey) as? String) ?? "No Country")
kABAddressCountryKey对应的值,打印出来,是一个国家。
我也需要country code,kABAddressCountryCodeKey对应的值,但是结果打印出来的是 "No Country"。
哪里错了呢?
代码没问题。
反复检查我的通讯录,2,020个有国家的联系人中,1,368没有国家编码。
再试验下,使用Mac的通讯录,取不到国家编码,使用手机通讯录倒是能取到。