以下是我的代码段
//Search Bar Delegate func searchBar(searchBar: UISearchBar,textDidChange searchText: String) { println(searchText) var predicate:NSPredicate=NSPredicate(format: "SELF CONTAINS[c] \(searchText)")! self.listItemToBeDisplayed=listItem.filteredArrayUsingPredicate(predicate) (self.view.viewWithTag(1) as UITableView).reloadData() }
我得到的错误:
*** Terminating app due to uncaught exception ‘NSUnknownKeyException’,reason: ‘[<__NSCFString 0x17405ef90> valueForUndefinedKey:]: this
class is not key value coding-compliant for the key V.’
强烈期望NSPredicate(格式:)与printf样式的格式字符串一起使用(它在插入参数时自动引用参数等).
原文链接:https://www.f2er.com/swift/318941.html你正在使用Swift的字符串插值,因此已经格式化的查询作为单个字符串来到NSPredicate.这使得它不会使用参数执行任何伏都教,从而使您遇到格式错误的查询.
改为使用printf样式格式:
if let predicate = NSPredicate(format: "SELF CONTAINS %@",searchText) { self.listItemToBeDisplayed = (listItem as NSArray).filteredArrayUsingPredicate(predicate) (self.view.viewWithTag(1) as UITableView).reloadData() }