首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >斯威夫特NSPredicate,名字和姓NSCompoundPredicate?

斯威夫特NSPredicate,名字和姓NSCompoundPredicate?
EN

Stack Overflow用户
提问于 2018-08-01 09:31:51
回答 2查看 711关注 0票数 0

在下面的代码中,我特别使用NSPredicates,我使用NSCompoundPredicate检查搜索函数中的多个不同参数。如何使用我目前正在使用的名字和姓氏进行搜索,但是它不会返回我的UITableView中的任何内容。我在复合谓词中列出的所有其他谓词都非常有用。

代码语言:javascript
复制
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等。

干杯!

EN

回答 2

Stack Overflow用户

发布于 2018-08-01 09:43:05

代码语言:javascript
复制
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当前逻辑:

代码语言:javascript
复制
if ((a == x AND b == x) OR (a == x) OR (b == x) OR (c = x)) {
}

(a == x AND b == x)是无用的。

这方面有一个问题:

代码语言:javascript
复制
let firstAndLast = NSPredicate(format: "firstName = %@ AND lastName = %@", textInSearchField)

您有两个占位符(%@),但只有一个参数(textInSearchField)。

代码语言:javascript
复制
let firstAndLast = NSPredicate(format: "firstName = %@ AND lastName = %@", textInSearchField, textInSearchField)
票数 0
EN

Stack Overflow用户

发布于 2018-08-01 10:51:25

感谢您的帮助,我最终使用了带有的另一种方法--我的代码如下所示,用于任何需要类似函数帮助的人,您可以通过字符(这适用于所有字符串,即名称或姓氏)进行搜索--在我的示例中,ID也是作为字符串存储的。

代码语言:javascript
复制
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上遵循了第二个答案

谢谢

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51629847

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档