gpt4 book ai didi

ios - 根据给定字符串从头开始搜索联系人

转载 作者:行者123 更新时间:2023-11-30 12:32:01 25 4
gpt4 key购买 nike

我想搜索联系人。我能够从“包含”中搜索具有匹配字符串的联系人。但我想搜索按给定搜索字符串开头的联系人。我的代码看起来像这样..

extension ContactViewController : UITextFieldDelegate {

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {

let searchString = (textField.text! as NSString).replacingCharacters(in: range, with: string)

if searchString == ""
{
contacts = allContacts
}
else
{
do {
let predicate: NSPredicate = CNContact.predicateForContacts(matchingName: searchString)
let keys = [CNContactFormatter.descriptorForRequiredKeys(for: .fullName),
CNContactImageDataKey,CNContactPhoneNumbersKey,CNContactImageDataAvailableKey] as [Any]
self.contacts = try self.contactStore.unifiedContacts(matching: predicate, keysToFetch:keys as! [CNKeyDescriptor])

}
catch {
print("Unable to refetch the selected contact.")
}
}

//contacts = filterContactArray(contact: contacts)
self.tableContact.reloadData()
return true
}

func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
}

所以请帮我解决这个问题。提前致谢。

最佳答案

以下是在 CNContact 中使用谓词的解决方案。

let predicates = NSPredicate(format: CNContactGivenNameKey + " BEGINSWITH[cd] %@", searchString)
contacts = (allContacts as NSArray).filtered(using: predicates) as! [CNContact]
tableContact.reloadData()

关于ios - 根据给定字符串从头开始搜索联系人,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43406986/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com