首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在swift4中将CNPhoneNumber转换为字符串?

如何在swift4中将CNPhoneNumber转换为字符串?
EN

Stack Overflow用户
提问于 2018-05-31 18:16:03
回答 2查看 2.7K关注 0票数 1

我正在使用此代码从联系人应用程序中获取联系人号码,但当我想在标签中显示号码时,会收到此警告,但不起作用:从'CNPhoneNumber‘转换为不相关的类型'String’总是失败

代码语言:javascript
复制
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
        }
    }
}
EN

回答 2

Stack Overflow用户

发布于 2018-05-31 19:37:46

你可以像CNLabeledValue一样获取PhoneNumber的值。

代码语言:javascript
复制
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)"
    }
}
票数 2
EN

Stack Overflow用户

发布于 2020-09-27 02:50:00

代码语言:javascript
复制
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")
    }
}

}

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50621917

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档