- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
在根据用户输入过滤我的 NSMutableDictionary 时,我创建了以下代码:
NSString *predicateString = [NSString stringWithFormat:@"SELF beginsWith[cd] %@", searchString];
NSPredicate *pred = [NSPredicate predicateWithFormat:predicateString];
NSArray *filteredKeys = [[myMutableDictionary allKeys] filteredArrayUsingPredicate:pred];
“searchString”被传递到具有此定义的方法中:
(NSString*) searchString
然而,这导致了以下异常:
...raised [ valueForUndefinedKey:]: this class is not key value coding-compliant for the key...
修复结果是:
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF beginsWith[cd] %@", searchString];
NSArray *filteredKeys = [[myMutableDictionary allKeys] filteredArrayUsingPredicate:pred];
我不明白的是,为什么后者有效,而前者抛出异常。我在key value coding上读了一点,但我不明白它在这里如何应用。 (即仅通过更改 NSPredicate 的定义方式)有人可以启发我吗?
更新:作为对 jtbandes 评论的回应,我继续创建了一个 TestApp 项目来演示这个问题。 http://dl.dropbox.com/u/401317/TestApp1.tar.gz
最佳答案
答案是in the predicate programming guide .
String constants must be quoted within the expression—single and double quotes are both acceptable, ... If you use variable substitution using %@ ..., the quotation marks are added for you automatically. If you use string constants within your format string, you must quote them yourself
[我的重点]
predicateWithFormat
会为您加上引号,但 stringWithFormat
不会。如果您这样做,您的第一个示例可能会起作用:
NSString *predicateString = [NSString stringWithFormat:@"SELF beginsWith[cd] '%@'", searchString];
// ^ ^ single or double quotes
关于objective-c - NSPredicate predicateWithFormat :(NSString*) inconsistency?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6726467/
获取错误 ORA-00932: inconsistent datatypes: expected CHAR got NUMBER 00932. 00000 - "inconsistent dataty
我的main.sass中有这个: #thing { -moz-box-sizing: border-box; -webkit-box-sizing: border
在 Python 中,为什么 [:] 切片操作的行为不一致? 它对于列表和字符串的行为有所不同。 对于列表,它给出一个副本列表对象,对于字符串,它给出相同的字符串对象。 我觉得这令人困惑,违反直觉。有
在检查和测试正则表达式的各个方面时,我偶然发现了一种奇怪且“不一致”的行为。我试图在正则表达式中使用一些代码,但同样的行为也适用于使用 void 代码块。 尤其是最让我感动的是,当我互换 :g 和 :
已连接两个支持蓝牙的设备。一个通过outputStream向另一个发送周期性时间戳(writeTime),另一个通过inputStream检索writeTimes并附加自己的时间戳(readTime)
我有以下距离矩阵: delta = [[ 0. 0.71370845 0.80903791 0.82955157 0.56964983 0. 0.
我正在使用 recyclerView 并将数据加载为 arrayList。如果 arrayList 少于 7 个项目,则不会发生崩溃。 否则,我会遇到这个 fatal error : java.lan
为什么结果是: double a = 0.0/0.0; double b = 0/0.0; = NaN 但是结果例如: double e = 0.1/0.0; double e = 12.0/0.0;
这是我的 Java 1.6 类: public class Foo { private ArrayList names; public void scan() { if (names
我正在制作一个使用 encog 预测足球比赛结果的程序。我创建了一个神经网络,使用弹性传播训练方法使用 90 场比赛的数据对其进行训练。我将比赛结果标记为 1 表示主场获胜,0 表示平局,-1 表示客
我正在向我的 App 类中正在进行的 WPF 应用程序添加一些可绑定(bind)的 CLR 属性,但由于这个不一致的可访问性错误,我无法编译。 Inconsistent Accessibility:
我正在尝试使用带有以下参数的 solve.QP 函数(来自 quadprog 包)运行优化 R = matrix( c( 2.231113e-05,-4.816095e-05,-5.115287e-0
我的 solr 架构中有以下两个字段: 当我在启用 facet 的情况下发出请求(faceting on brand_id) http://example.com/solr/select?wt=j
我在 UIView 上有一个 UIButton。我想以编程方式确定显示 View 时在按钮内显示哪个图像。我已经重写了 UIView 中的 drawRect 方法并使用 setImage 来显示所需的
在常规中: println 'test' as Boolean //true println 'test'.toBoolean() //false println new Boolean('test'
例如,在 message.properties 中空白字段的默认 i18n 消息是: default.blank.message=Property [{0}] of class [{1}] canno
我正在尝试使用 array_multisort() 在其子数组的基础上对数组进行排序功能...... 在尝试的同时; print_r($mar); echo ''; $arr2 = array_mul
我正在使用 MALLET 来训练 ParallelTopicModel。训练后,我得到一个 TopicInferencer,取一个句子,通过推理器运行 15 次,然后检查结果。我发现对于某些主题,每次
1) 为什么在 JavaScript 中存在这种不一致 - 我期望第四行也返回 11: (function(n, m) { n = n + m; return n })(3, 8)
上下文: 我有一个小部件,它基本上由一个包装了一堆 TextView 的 RelativeLayout 组成。这是我希望小部件的外观,然后是 XML 布局代码: 问题:我
我是一名优秀的程序员,十分优秀!