arrays – 谓词来过滤SWIFT中的字符串数组会引发错误,说NSCFString不是键值编码

以下是我的代码段
//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()
}

相关文章

软件简介:蓝湖辅助工具,减少移动端开发中控件属性的复制和粘...
现实生活中,我们听到的声音都是时间连续的,我们称为这种信...
前言最近在B站上看到一个漂亮的仙女姐姐跳舞视频,循环看了亿...
【Android App】实战项目之仿抖音的短视频分享App(附源码和...
前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至...
因为我既对接过session、cookie,也对接过JWT,今年因为工作...