【问题标题】:Extra argument call in SwiftSwift 中的额外参数调用
【发布时间】:2020-07-27 17:53:47
【问题描述】:

错误如下:Extra Argument "predicate" in Call
我正在使用它在应用程序中的数组中进行搜索。

代码是:

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)
}

感谢任何帮助。

【问题讨论】:

  • 你是怎么调用这个函数的?
  • 根据代码和错误信息loadItems没有第二个参数。您可以将谓词分配给请求。
  • 在加载项中。最后一行。
  • 为什么不将谓词分配给请求? request.predicate = predicate
  • 会调查的。谢谢。

标签: ios swift xcode uitableview


【解决方案1】:

您必须将谓词分配给请求。

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)

   self.tableView.reloadData() //for reloading the table view
}

这应该可以正常工作。

【讨论】:

    【解决方案2】:

    谓词是请求上的一个属性,也许你的意思是

    func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
       let request: NSFetchRequest<Item> = Item.fetchRequest()
       
       let predicate = NSPredicate(format: "title CONTAINS[cd] %@", searchBar.text!)
       request.predicate = predicate
       request.sortDescriptors = [NSSortDescriptor(key: "title", ascending: true)]
    
       loadItems(with: request)
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-21
      • 1970-01-01
      • 2015-09-13
      • 2018-08-26
      • 2017-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多