以下是我的代码段
//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样式的格式字符串一起使用(它在插入参数时自动引用参数等).
你正在使用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()
}