use*_*792 6 arrays nsarray nspredicate swift
以下是我的代码段
//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()
}
Run Code Online (Sandbox Code Playgroud)
我得到的错误:
***由于未捕获的异常'NSUnknownKeyException'终止应用程序,原因:'[<__ NSCFString 0x17405ef90> valueForUndefinedKey:]:此类不是密钥V的密钥值编码兼容的.
我想过滤数组中的字符串以通过我的搜索字符串进行过滤.如果我的搜索字符串包含在数组中的任何字符串中,则应该列出它.
ric*_*ter 13
NSPredicate(format:) 强烈期望与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()
}
Run Code Online (Sandbox Code Playgroud)