swift - 如何在 swift4 中将 CNPhoneNumber 转换为字符串?
问题描述
我正在使用此代码从联系人应用程序获取联系人号码,但是当我想在标签中显示号码时,我收到此警告并且不起作用:从“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
}
}
}
解决方案
尝试 :
if let phone = number.value as? CNPhoneNumber {
print(phone.stringValue)
} else {
print("number.value not of type CNPhoneNumber")
}
推荐阅读
- android - 如何查看使用 Android 模拟器(没有 Android Studio)运行应用程序时发生的错误的堆栈跟踪
- caching - google 将 amp-live-list 的旧数据缓存多长时间
- dart - 如何使用 SingleChildScrollView 使 Stack 布局可滚动?
- javascript - 在 React Native 中处理提交 Redux 表单不起作用(没有值打印出来)
- javascript - 当我对同一浏览器上的另一个选项卡进行任何更改时如何刷新选项卡数据
- ssl - Tomcat 没有获取密钥库
- android - 使用 android 默认文件夹将视频保存到公共文件夹不会出现在图库中
- jmeter - 需要检查“响应数据”中的“字符串”
- grpc - 处理多种类型请求的异步 grpc 服务器
- android - TabLayout -> View pager -> Fragments -> Recyclerview ,在这个层次结构中 Recyclerview 不可见