加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

arrays – 谓词来过滤SWIFT中的字符串数组会引发错误,说NSCFStri

发布时间:2020-12-14 05:21:48 所属栏目:百科 来源:网络整理
导读:以下是我的代码段 //Search Bar Delegatefunc searchBar(searchBar: UISearchBar,textDidChange searchText: String){ println(searchText) var predicate:NSPredicate=NSPredicate(format: "SELF CONTAINS[c] (searchText)")! self.listItemToBeDisplayed=
以下是我的代码段
//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()
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读