我正在使用此代码从联系人应用程序获取联系号码,但是当我想在标签中显示号码时,我收到此警告并且不起作用:从“CNPhoneNumber”转换为不相关的类型“字符串”总是失败
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
}
}
}
Run Code Online (Sandbox Code Playgroud)
尝试 :
if let phone = number.value as? CNPhoneNumber {
print(phone.stringValue)
} else {
print("number.value not of type CNPhoneNumber")
}
Run Code Online (Sandbox Code Playgroud)
也看看CNContact , CNPhoneNumber