如何在 swift4 中将 CNPhoneNumber 转换为字符串?
how to convert a CNPhoneNumber to string in swift4?
我正在使用此代码从联系人应用程序获取联系电话号码,但是当我想在标签中显示电话号码时,我收到此警告并且不起作用:
从 '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
}
}
}
尝试:
if let phone = number.value as? CNPhoneNumber {
print(phone.stringValue)
} else {
print("number.value not of type CNPhoneNumber")
}
您可以获取 PhoneNumber 值作为 CNLabeledValue
。
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)"
}
}
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")
}
}
}
我正在使用此代码从联系人应用程序获取联系电话号码,但是当我想在标签中显示电话号码时,我收到此警告并且不起作用: 从 '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
}
}
}
尝试:
if let phone = number.value as? CNPhoneNumber {
print(phone.stringValue)
} else {
print("number.value not of type CNPhoneNumber")
}
您可以获取 PhoneNumber 值作为 CNLabeledValue
。
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)"
}
}
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")
}
}
}