如何使用联系人与Swiftsorting联系人

我已阅读有关sorting联系人的官方苹果文档,但我不知道如何实现它。 所以,这里是获取请求:

let fetchRequest = CNContactFetchRequest(keysToFetch: keysToFetch) 

和我最喜欢的sorting顺序:

 let sortOrder = CNContactSortOrder.UserDefault 

这是我通常如何获取联系人:

  do { try store.enumerateContactsWithFetchRequest(fetchRequest, usingBlock: { (let contact, let stop) -> Void in self.contacts.append(contact) }) } catch let error as NSError { print(error.localizedDescription) } 

现在我应该怎么做sortOrder ? 我应该在什么地方包括我的整个提取过程?

更新为Swift 4.0

 let fetchRequest = CNContactFetchRequest(keysToFetch: [CNContactGivenNameKey as CNKeyDescriptor, CNContactFamilyNameKey as CNKeyDescriptor, CNContactMiddleNameKey as CNKeyDescriptor, CNContactEmailAddressesKey as CNKeyDescriptor,CNContactPhoneNumbersKey as CNKeyDescriptor]) fetchRequest.sortOrder = CNContactSortOrder.userDefault let store = CNContactStore() do { try store.enumerateContacts(with: fetchRequest, usingBlock: { (contact, stop) -> Void in // print(contact.phoneNumbers.first?.value ?? "not found") }) } catch let error as NSError { print(error.localizedDescription) } 

老版本是这样写的

  fetchRequest.sortOrder = CNContactSortOrder.UserDefault 

在创buildfetchRequest对象之后,你的最终输出是这样的

 let fetchRequest = CNContactFetchRequest(keysToFetch: keysToFetch) fetchRequest.sortOrder = CNContactSortOrder.UserDefault do { try store.enumerateContactsWithFetchRequest(fetchRequest, usingBlock: { (let contact, let stop) -> Void in self.contacts.append(contact) }) } catch let error as NSError { print(error.localizedDescription) }