我正在使用fetchedResultsController来填充带有名称列表的tableview . 加载视图时,tableView使用Core Data中存储的名称正确填充 .

func attemptFetch() {

    let fetchRequest: NSFetchRequest<Respondent> = Respondent.fetchRequest()
    let lastSort = NSSortDescriptor(key: "last", ascending: true)
    let firstSort = NSSortDescriptor(key: "first", ascending: true)

    fetchRequest.sortDescriptors = [lastSort, firstSort]

    let controller = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: context, sectionNameKeyPath: nil, cacheName: "firstCache")

    controller.delegate = self

    self.controller = controller

    do {
        try controller.performFetch()
    } catch {
        let error = error as NSError
        print ("\(error)")
    }
}

在设备上显示:

我遇到的问题是,当我使用searchBar函数搜索第一个名称,并使用此文本设置谓词时,performFetch失败,表明我给出了一个无效的参数 . 以下是我的功能:

func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
    if let textInSearchBar = searchBar.text {

        let fetchRequest: NSFetchRequest<Respondent> = Respondent.fetchRequest()

        //set up sortDescriptor for fetch
        let lastSort = NSSortDescriptor(key: "last", ascending: true)
        let firstSort = NSSortDescriptor(key: "first", ascending: true)
        fetchRequest.sortDescriptors = [lastSort, firstSort]

        let searchPredicate = NSPredicate(format: "first contains[c] %@", textInSearchBar)

        fetchRequest.predicate = searchPredicate

        let controller = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: context, sectionNameKeyPath: nil, cacheName: nil)

        controller.delegate = self

        self.controller = controller

        do {
            try self.controller.performFetch()
        } catch {
            let error = error as NSError
            print ("\(error)")
        }
        tableView.reloadData()
    }
}

我得到的错误是:

2018-12-08 15:33:09.100555-0700软触摸[83481:3507366] [错误]错误:SQLCore dispatchRequest:异常处理请求:,谓词的未实现SQL生成:(FIRST CONTAINS [c]“John”)(bad使用(null)CoreData的userInfo的RHS):错误:SQLCore dispatchRequest:异常处理请求:,谓词的未实现的SQL生成:(FIRST CONTAINS [c]“John”)(坏RHS),userInfo为(null)2018-12- 08 15:33:09.115905-0700软触摸[83481:3507366] ***由于未捕获的异常'NSInvalidArgumentException'终止应用程序,原因:'谓词的未实现的SQL生成:(FIRST CONTAINS [c]“John”)(错误的RHS )”

这让我感到困惑,因为我正确地创造了它 . 我也知道我在我的Respondent核心数据类中首先拥有String属性:

我不确定我哪里出错了 . 谢谢 .