swift - 使用无法在 swift 3 上运行的多部手机创建新联系人
问题描述
我想用多部手机创建新联系人。我不知道为什么,但我的快速代码只创建了 1 个电话号码。与创建新联系人相关的示例很少,所以我想我的问题会对其他人有所帮助。
private func createNewContact(myContact : AgregarContactoViewModel) {
let store = CNContactStore()
let contact = CNMutableContact()
// Name
contact.familyName = myContact.getName()
// Phones
for i in (0..<myContact.getPhones().count) {
let phone = CNLabeledValue(label: CNLabelOther, value: CNPhoneNumber(stringValue: myContact.getPhones()[i]))
contact.phoneNumbers.append(phone)
}
// Call the controller and create new contact
let controller = CNContactViewController(forNewContact : contact)
controller.contactStore = store
controller.delegate = self
self.navigationController?.setNavigationBarHidden(false, animated: true)
self.navigationController?.pushViewController(controller, animated: true)
}
谢谢
解决方案
我使用Matt Neuburg编写的示例代码Programming-iOS-Book-Examples找到了解决方案:
代替
let phone = CNLabeledValue(label: CNLabelOther, value: CNPhoneNumber(stringValue: myContact.getPhones()[i]))
contact.phoneNumbers.append(phone)
解决方案是:
contact.phoneNumbers.append(CNLabeledValue(label: "phone", value: CNPhoneNumber(stringValue: myContact.getPhones()[i])))
感谢 Matt Neuburg 的评论和良好的编码!
推荐阅读
- javascript - 每当页面刷新时,Java Script Count Down Timer 都会重置
- javascript - 如何为 jquery i18n 设置字符集 utf-8?
- javascript - IE11中的Array.map()“语法错误”,映射多个JSON对象字段
- python - 在python中将字符串的十六进制字符转换为普通字符
- java - 未找到允许在生产环境中获得最佳性能的基于 APR 的 Apache Tomcat Native 库
- postgresql - 使用按月划分的 PostgreSQL 表分区
- c - 关于 fopen 中的二进制模式
- vim - 如何在 vim 中定义键映射并多次使用重复编号?
- android - 无法从一个活动获取数据到另一个活动
- batch-file - 如何在批处理脚本中检查互联网连接状态