使用获取结果控制器(FRC)在搜索栏中搜索第一个字符时,nspredicate出现问题。
用于updatesearchresult的搜索栏委托:
//UISearchResultsUpdating
public func updateSearchResults(for searchController: UISearchController){
if let searchText = resultSearchController.searchBar.text , searchController.isActive { Other codes........ }
}
我的谓词如下:
let entity = NSEntityDescription.entity(forEntityName: "XMPPUserCoreDataStorageObject", in: moc)!
let sd1 = NSSortDescriptor(key: "nickname", ascending: false)
let sortDescriptors = [sd1]
let fetchRequest:NSFetchRequest<XMPPUserCoreDataStorageObject> = NSFetchRequest(entityName: "XMPPUserCoreDataStorageObject")
fetchRequest.entity = entity
fetchRequest.sortDescriptors = sortDescriptors
fetchRequest.fetchBatchSize = 10
*fetchRequest.predicate = NSPredicate(format:"jidStr contains[c] %@ OR nickname contains[c] %@ ", searchText,searchText)*
我使用自定义sectionkeypath(“ searchSection”)来分隔 Contacts 和 Group Chats 部分,如下所示
let fetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext:moc , sectionNameKeyPath: "searchSection", cacheName: nil)
对于searchSection,声明如下
dynamic var searchSection: String {
if(jidStr.contains("+")){
return "Contacts"
}else{
return "Group Chats"
}
}
您可以在下面参考所附的图片
我想知道为什么当输入第二个字符时会出现结果,但是对于某些字符却没有第一个字符。希望任何人都能提供帮助。非常感谢