如何使用Swift联系人对联系人进行排序 [英] How to sort contacts using Contacts with Swift

查看:175
本文介绍了如何使用Swift联系人对联系人进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了有关排序联系人的官方苹果文档,但我不确定如何实现它。
所以,这里是获取请求:

 让fetchRequest = CNContactFetchRequest(keysToFetch:keysToFetch)

和我的首选排序顺序:

  let sortOrder = CNContactSortOrder.UserDefault 

这就是我通常获取联系人的方式:

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

现在我该如何处理 sortOrder ?我应该在整个提取过程中包括哪些内容?

解决方案

更新为Swift 4.0

 让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)

})
}
捕捉错误为NSError {
print(error.localizedDescription)
}

旧版
写得像这样

  fetchRequest.sortOrder = CNContactSortOrder.UserDefault 


fetchRequest对象创建后
所以你的最终输出就像

 让fetchRequest = CNContactFetchRequest(keysToFetch:keysToFetch)

fetchRequest.sortOrder = CNContactSortOrder.UserDefault

do {
try store.enumerateContactsWithFetchRequest(fetchRequest,usingBlock :{(让联系,让我们停止) - >无效
self.contacts.append(联系人)
})
}
捕捉错误为NSError {
print(error.localizedDescription)
}


I've read official apple documentation about sorting contacts, although I am not sure how to implement it. So, here is fetch request:

let fetchRequest = CNContactFetchRequest(keysToFetch: keysToFetch)

and my prefered sort order:

let sortOrder = CNContactSortOrder.UserDefault

and this is how I usually fetch contacts:

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

Now what should I do with sortOrder? Where and should I include in my whole fetching process?

解决方案

Updated For 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)
        }

Old Version write like this

 fetchRequest.sortOrder = CNContactSortOrder.UserDefault

after fetchRequest object created so your final output is like

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)
    }

这篇关于如何使用Swift联系人对联系人进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆