gpt4 book ai didi

ios - 使用 CNContacts (Swift) 实现搜索 Controller 时出现问题

转载 作者:行者123 更新时间:2023-11-29 05:08:16 25 4
gpt4 key购买 nike

我正在开发一个小应用程序,我想实现一个搜索 Controller ,以便能够查询我检索到的联系人。我已成功检索系统联系人并将其显示在表格 View 中。但是,当我尝试实现搜索 Controller 来简化联系人搜索时,我遇到了“无效谓词”问题,并且我不知道如何解决它。

这是我的实际方法:

import UIKit
import Contacts

class PhoneContactsVC: UITableViewController {

var contactsArray = [CNContact]()
var contacts = [CNContact]()
var selectedContactCN = CNContact()
var searchController: UISearchController!
var searchResults = [CNContact]()

override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self as UITableViewDelegate
tableView.dataSource = self
let store = CNContactStore()
retrieveContacts(from: store)
setColors()
let searchResultsController = UITableViewController(style: .insetGrouped)
searchResultsController.tableView.delegate = self
searchResultsController.tableView.dataSource = self
searchResultsController.tableView.rowHeight = 63
searchResultsController.tableView.register(FriendCell.self, forCellReuseIdentifier: "FriendCell")
searchController = UISearchController(searchResultsController: searchResultsController)
searchController.searchResultsUpdater = self
searchController.searchBar.sizeToFit()
searchController.searchBar.tintColor = .systemGreen
searchController.searchBar.delegate = self
searchController.searchBar.barTintColor = .systemGreen
definesPresentationContext = true
navigationItem.searchController = searchController

}

override func willMove(toParent parent: UIViewController?) { // tricky part in iOS 10
navigationController?.navigationBar.barTintColor = UIColor.systemGreen //previous color
super.willMove(toParent: parent)
}

private func setColors(){
navigationController?.navigationBar.barTintColor = UIColor.white
let textAttributes = [NSAttributedString.Key.foregroundColor:UIColor.black]
navigationController?.navigationBar.titleTextAttributes = textAttributes
navigationController?.navigationBar.tintColor = UIColor.systemBlue;
}

func filterResultsWithSearchString(searchString: String) {
let predicate: NSPredicate = CNContact.predicateForContacts(matchingName: searchString)
let keysToFetch = [CNContactGivenNameKey as CNKeyDescriptor, CNContactFamilyNameKey as CNKeyDescriptor, CNContactImageDataAvailableKey as CNKeyDescriptor, CNContactBirthdayKey as CNKeyDescriptor, CNContactImageDataKey as CNKeyDescriptor, CNContactPhoneNumbersKey as CNKeyDescriptor]
searchResults = try! CNContactStore().unifiedContacts(matching: predicate, keysToFetch: keysToFetch)

}


func retrieveContacts(from store: CNContactStore) {
let containerId = store.defaultContainerIdentifier()
let predicate = CNContact.predicateForContactsInContainer(withIdentifier: containerId)
// 4
let keysToFetch = [CNContactGivenNameKey as CNKeyDescriptor, CNContactFamilyNameKey as CNKeyDescriptor, CNContactImageDataAvailableKey as CNKeyDescriptor, CNContactBirthdayKey as CNKeyDescriptor, CNContactImageDataKey as CNKeyDescriptor, CNContactPhoneNumbersKey as CNKeyDescriptor]
contactsArray = try! store.unifiedContacts(matching: predicate, keysToFetch: keysToFetch)
contacts = contactsArray.sorted(by: {$0.givenName < $1.givenName})
contacts.removeAll(where: {$0.givenName == ""})
DispatchQueue.main.async { [weak self] in
self?.tableView.reloadData()
}
}
}

extension PhoneContactsVC: UISearchBarDelegate {
}

extension PhoneContactsVC: UISearchResultsUpdating {
func updateSearchResults(for searchController: UISearchController) {
let searchString = searchController.searchBar.text!
filterResultsWithSearchString(searchString: searchString)

let searchResultsController = searchController.searchResultsController as! UITableViewController
searchResultsController.tableView.reloadData()
}
}

extension PhoneContactsVC {
// 1
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return contacts.count
}

// 2
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// 3
let cell = self.tableView.dequeueReusableCell(withIdentifier: "ContactTableViewCell") as! ContactTableViewCell
let contact = searchController.isActive ? searchResults[indexPath.row] : contacts[indexPath.row]

if contact.birthday == nil {
cell.registeredBday.isHidden = true
}
else {
cell.registeredBday.isHidden = false
}
cell.nameLabel.text = "\(contact.givenName) \(contact.familyName)"

// 4
if contact.imageDataAvailable == true, let imageData = contact.imageData {
cell.contactImage.image = UIImage(data: imageData)
}
else {
cell.contactImage.image = UIImage(named: "MaLong")
}
return cell
}

override func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath{
selectedContactCN = contacts[indexPath.row]
return indexPath
}
}

希望您能给我一个解决方案。谢谢大家!

最佳答案

..因为你的谓词无效=)。更新代码如下:

    func filterResultsWithSearchString(searchString: String) {
let keysToFetch = [CNContactGivenNameKey as CNKeyDescriptor, CNContactFamilyNameKey as CNKeyDescriptor, CNContactImageDataAvailableKey as CNKeyDescriptor, CNContactBirthdayKey as CNKeyDescriptor, CNContactImageDataKey as CNKeyDescriptor, CNContactPhoneNumbersKey as CNKeyDescriptor]
var predicate: NSPredicate!
if searchString.isEmpty {
let containerId = CNContactStore().defaultContainerIdentifier()
predicate = CNContact.predicateForContactsInContainer(withIdentifier: containerId)

}
else {
predicate = CNContact.predicateForContacts(matchingName: searchString)
}
do {
searchResults = try CNContactStore().unifiedContacts(matching: predicate, keysToFetch: keysToFetch)
}
catch let error {
print("ERROR: \(error)")
}
}

...

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return searchController.isActive ? searchResults.count : contacts.count
}

关于ios - 使用 CNContacts (Swift) 实现搜索 Controller 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59949712/

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