predicate来过滤SWIFT字符串数组抛出错误说NSCFString不是键 - 值编码 [英] Predicate to filter array of strings in SWIFT throws error saying NSCFString is not key-value coding

查看:198
本文介绍了predicate来过滤SWIFT字符串数组抛出错误说NSCFString不是键 - 值编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的code段

//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()

}

错误我:

***终止应用程序由于未捕获的异常'NSUnknownKeyException',原因:'[< __ NSCFString 0x17405ef90> valueForUndefinedKey:]:这
  类不是键值为重点V标准与编码。

*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<__NSCFString 0x17405ef90> valueForUndefinedKey:]: this class is not key value coding-compliant for the key V.'

我想在阵列过滤字符串我的搜索字符串进行过滤。如果我的搜索字符串包含在任何数组中的字符串,那么它应该被列出。

I want to filter strings in array to be filtered by my search string. If my search string is contained in any of the string in array, then it should be listed.

推荐答案

NS predicate(格式:)强烈期待与printf风格的格式字符串中使用(因为它插入他们,等它自动报价参数)。

NSPredicate(format:) strongly expects to be used with printf-style format strings (it automatically quotes arguments as it inserts them, etc).

您使用的是斯威夫特的串插,所以本已格式化的查询涉及到 NS predicate 作为一个字符串。这令它做什么巫术它的参数呢,留下一个畸形的查询。

You're using Swift's string interpolation, so the already-formatted query comes to NSPredicate as a single string. That keeps it from doing whatever voodoo it does with arguments, leaving you with a malformed query.

使用printf风格的格式,而不是:

Use printf-style formatting instead:

if let predicate = NSPredicate(format: "SELF CONTAINS %@", searchText) {
    self.listItemToBeDisplayed = (listItem as NSArray).filteredArrayUsingPredicate(predicate)
    (self.view.viewWithTag(1) as UITableView).reloadData()
}

这篇关于predicate来过滤SWIFT字符串数组抛出错误说NSCFString不是键 - 值编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆