如何在Swift中获取CNContact电话号码作为字符串? [英] How to get a CNContact phone number(s) as string in Swift?
问题描述
我正在尝试检索所有联系人的姓名和电话号码,并在iOS中使用Swift将它们放入数组中。我已经做到了这一点:
I am attempting to retrieve the names and phone number(s) of all contacts and put them into arrays with Swift in iOS. I have made it this far:
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
会返回一个值数组,例如
Once completed, marrContactsName
contains an array of all my contacts' names exactly as expected. i.e. "John Doe". However, marrContactsNumber
returns an array of values like
[<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>>]
我想知道如何检索JUST电话号码作为字符串值,即XXXXXXXXXX。基本上,如何调用数字值。谢谢!
I would like to know how to retrieve JUST the phone number(s) as a string value(s) i.e. "XXXXXXXXXX". Basically, how to call for the digit(s) value. Thanks!
推荐答案
我找到了解决方案:(contact.phoneNumbers [0] .value as!CNPhoneNumber ).valueForKey(数字)为!字符串
这篇关于如何在Swift中获取CNContact电话号码作为字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!