在下面的代码中,我特别使用NSPredicates,我使用NSCompoundPredicate检查搜索函数中的多个不同参数。如何使用我目前正在使用的名字和姓氏进行搜索,但是它不会返回我的UITableView中的任何内容。我在复合谓词中列出的所有其他谓词都非常有用。
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
textInSearchField = searchBar.text!
if searchBar.text == ""{
print("Searching for all clients.")
retrieveClients()
view.endEditing(true)
clientTableView.reloadData()
}else{
print("Enter a client name to search.")
isFilteringSearch = true
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let context = appDelegate.persistentContainer.viewContext
let firstAndLast = NSPredicate(format: "firstName = %@ AND lastName = %@", textInSearchField)
let firstNamePredicate = NSPredicate(format: "firstName = %@", textInSearchField)
let lastNamePredicate = NSPredicate(format: "lastName = %@", textInSearchField)
let idPredicate = NSPredicate(format: "id = %@", textInSearchField)
let orPredicate = NSCompoundPredicate(type: NSCompoundPredicate.LogicalType.or, subpredicates: [firstAndLast,firstNamePredicate, lastNamePredicate, idPredicate])
clientsEntity.predicate = orPredicate
clients = try! context.fetch(clientsEntity) as! [NSManagedObject]
view.endEditing(true)
clientTableView.reloadData()
}}
要记住的一点是,我仍然需要能够使用LogicalType.or,因为我希望用户可以选择按名、姓进行搜索,但同时也需要同时使用Harold 或Finch/Harold等。
干杯!
发布于 2018-08-01 09:43:05
let firstAndLast = NSPredicate(format: "firstName = %@ AND lastName = %@", textInSearchField)
let firstNamePredicate = NSPredicate(format: "firstName = %@", textInSearchField)
let lastNamePredicate = NSPredicate(format: "lastName = %@", textInSearchField)
let idPredicate = NSPredicate(format: "id = %@", textInSearchField)
let orPredicate = NSCompoundPredicate(type: NSCompoundPredicate.LogicalType.or, subpredicates: [firstAndLast,firstNamePredicate, lastNamePredicate, idPredicate])我希望对orPredicate来说,它应该是[firstAndLast, idPredicate],但是您没有让它工作。否则,您需要重新考虑,这是您的orPredicate当前逻辑:
if ((a == x AND b == x) OR (a == x) OR (b == x) OR (c = x)) {
}(a == x AND b == x)是无用的。
这方面有一个问题:
let firstAndLast = NSPredicate(format: "firstName = %@ AND lastName = %@", textInSearchField)您有两个占位符(%@),但只有一个参数(textInSearchField)。
let firstAndLast = NSPredicate(format: "firstName = %@ AND lastName = %@", textInSearchField, textInSearchField)发布于 2018-08-01 10:51:25
感谢您的帮助,我最终使用了带有的另一种方法--我的代码如下所示,用于任何需要类似函数帮助的人,您可以通过字符(这适用于所有字符串,即名称或姓氏)进行搜索--在我的示例中,ID也是作为字符串存储的。
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
textInSearchField = searchBar.text!
if searchBar.text == ""{
retrieveClients()
view.endEditing(true)
clientTableView.reloadData()
}else{
isFilteringSearch = true
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let context = appDelegate.persistentContainer.viewContext
let words = textInSearchField.components(separatedBy: " ")
for word in words{
if (word).count == 0{
continue
}
let firstNamePredicate = NSPredicate(format: "firstName contains[c] %@", word)
let lastNamePredicate = NSPredicate(format: "lastName contains[c] %@", word)
let idPredicate = NSPredicate(format: "id contains[c] %@", word)
let orPredicate = NSCompoundPredicate(type: NSCompoundPredicate.LogicalType.or, subpredicates: [firstNamePredicate, lastNamePredicate, idPredicate])
clientsEntity.predicate = orPredicate
}
clients = try! context.fetch(clientsEntity) as! [NSManagedObject]
view.endEditing(true)
clientTableView.reloadData()
}}
我在这篇文章NSPredicate: Combine CONTAINS with IN上遵循了第二个答案
谢谢
https://stackoverflow.com/questions/51629847
复制相似问题