Swift 调用中的额外参数 'predicate'

Extra argument 'predicate' in call in Swift

func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
   let request: NSFetchRequest<Item> = Item.fetchRequest()
   print(searchBar.text!)

   let predicate = NSPredicate(format: "title CONTAINS[cd] %@", searchBar.text!)

   request.sortDescriptors = [NSSortDescriptor(key: "title", ascending: true)]

   loadItems(with: request, predicate: predicate)
}

错误是"Extra argument 'predicate' in call"。 如果我删除谓词参数,则不会在项目数组中进行搜索。 这是在 table 视图中。 您可以在文件末尾的 GitHub 查看完整代码。

您必须将谓词分配给请求

func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
   let request: NSFetchRequest<Item> = Item.fetchRequest()
   print(searchBar.text!)

   request.predicate = NSPredicate(format: "title CONTAINS[cd] %@", searchBar.text!)

   request.sortDescriptors = [NSSortDescriptor(key: "title", ascending: true)]

   loadItems(with: request)
}