如果您已经列举过接触,就不用创建假的接触。您可以直接使用@ user6295172建议的第二行。以下是我们项目中的一个例子。
public func enumerateContacts() -> Void {
let contactStore = CNContactStore()
let filterFields = [CNContactFormatter.descriptorForRequiredKeys(for: .fullName), CNContactPhoneNumbersKey] as [Any]
let fetchRequest = CNContactFetchRequest(keysToFetch: filterFields as! [CNKeyDescriptor])
do {
try contactStore.enumerateContacts(with: fetchRequest) { contact, stop in
if contact.phoneNumbers.count == 0 {
// No phone number associated with the contact
// worth skipping it or do what you feel like
}
if CNContactFormatter.nameOrder(for: contact).rawValue == 0 {
print(contact.identifier, " : ", contact.familyName, contact.givenName)
} else {
print(contact.identifier, " : ", contact.givenName, contact.familyName)
}
for phone in contact.phoneNumbers {
print(phone.identifier, " : ", phone.value.stringValue)
}
print("\n")
}
} catch let err {
print(err)
}
}
你是什么意思创建一个联系对象?您的代码看起来不像创建联系人对象。 – bcamur