我正在使用此代码从联系人应用程序中获取联系人号码,但当我想在标签中显示号码时,会收到此警告,但不起作用:从'CNPhoneNumber‘转换为不相关的类型'String’总是失败
func contactPicker(_ picker: CNContactPickerViewController, didSelect contacts: [CNContact]) {
contacts.forEach {(contact) in
for number in contact.phoneNumbers{
let phone = number.value
print(phone)
numberLabel.text = phone as! String
}
}
}
发布于 2018-05-31 19:37:46
你可以像CNLabeledValue
一样获取PhoneNumber的值。
for number in contact.phoneNumbers{
if let number = phoneNumber.value as? CNPhoneNumber, let phoneLabel = phoneNumber.label {
let phoneLocalizedLabel = CNLabeledValue.localizedStringForLabel(phoneLabel)
numberLabel.text = "\(phoneLocalizedLabel) : \(number.stringValue)"
}
}
发布于 2020-09-27 02:50:00
func getContact(){
let numeroinc = self.numero.text!
let contactStore = CNContactStore()
var numeroScodeCuntry = ""
let keys = [
CNContactFormatter.descriptorForRequiredKeys(for: .fullName),
CNContactPhoneNumbersKey,
CNContactEmailAddressesKey
] as [Any]
let request = CNContactFetchRequest(keysToFetch: keys as! [CNKeyDescriptor])
for char in UserDefaults.standard.string( forKey: "cc" )!.count ... numeroinc.count-1 {
let index = numeroinc.index(numeroinc.startIndex, offsetBy: char)
numeroScodeCuntry.append(numeroinc[index])
}
numeroScodeCuntry = formattedNumber2(number:numeroScodeCuntry)
print("numeroScodeCuntry", numeroScodeCuntry)
do {
try contactStore.enumerateContacts(with: request){
(contact, stop) in
// Array containing all unified contacts from everywhere
self.contacts.append(contact)
for phoneNumber in contact.phoneNumbers {
if let number = phoneNumber.value as? CNPhoneNumber, let label = phoneNumber.label {
let localizedLabel = CNLabeledValue<CNPhoneNumber>.localizedString(forLabel: label)
print("\(contact.givenName) \(contact.familyName) tel:\(localizedLabel) -- \(number.stringValue), email: \(contact.emailAddresses)")
print("numeroScodeCuntry",numeroScodeCuntry ,"number.stringValue" , number.stringValue)
let numberContact = self.formattedNumber2(number:number.stringValue)
if (numeroScodeCuntry.isEqualToString(find: numberContact)){
self.numero.text = contact.givenName
print("contact.givenName",contact.givenName)
self.isNumber = false
break
}else{
self.isNumber = true
}
}
}
}
print(self.contacts)
} catch {
print("unable to fetch contacts")
}
}
}
https://stackoverflow.com/questions/50621917
复制相似问题