我正在尝试检索所有联系人的姓名和电话号码,并在 iOS 中使用 Swift 将它们放入数组中。我已经做到了这一点:
func findContacts() -> [CNContact] {
marrContactsNumber.removeAllObjects()
marrContactsName.removeAllObjects()
let store = CNContactStore()
let keysToFetch = [CNContactGivenNameKey, CNContactFamilyNameKey, CNContactPhoneNumbersKey]
let fetchRequest = CNContactFetchRequest(keysToFetch: keysToFetch)
var contacts = [CNContact]()
do {
try store.enumerateContactsWithFetchRequest(fetchRequest, usingBlock: { (let contact, let stop) -> Void in
contacts.append(contact)
self.marrContactsName.addObject(contact.givenName + " " + contact.familyName)
self.marrContactsNumber.addObject(contact.phoneNumbers)
print(contact.phoneNumbers)
}
catch let error as NSError {
print(error.localizedDescription)
}
print(marrContactsName.count)
print(marrContactsNumber.count)
return contacts
}
完成后,marrContactsName
包含我所有联系人姓名的数组,完全符合预期。即“John Doe”。但是,marrContactsNumber
返回一组值,如
[<CNLabeledValue: 0x158a19950: identifier=F831DC7E-5896-420F-AE46-489F6C14DA6E,
label=_$!<Work>!$_, value=<CNPhoneNumber: 0x158a19640: countryCode=us, digits=6751420000>>,
<CNLabeledValue: 0x158a19a80: identifier=ECD66568-C6DD-441D-9448-BDEDDE9A68E1,
label=_$!<Work>!$_, value=<CNPhoneNumber: 0x158a199b0: countryCode=us, digits=5342766455>>]
我想知道如何只检索电话号码作为字符串值,即“XXXXXXXXXX”。基本上,如何调用数字值。谢谢!
最佳答案
我找到了解决方案:(contact.phoneNumbers[0].value as!CNPhoneNumber).valueForKey("digits") as!字符串
关于ios - 如何在 Swift 中获取 CNContact 电话号码作为字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36343312/