ios - 通过 NSPredicate 过滤数组时出现 NSUnknownKeyException

标签 ios nsstring nspredicate nsunknownkeyexception

我有一个非常烦人的错误。可能有一个简单的解决方案,但我无法得到它。

我使用 NSPredicate通常没有任何问题。现在出现以下错误:Terminating app due to uncaught exception ' NSUnknownKeyException ',原因:'[<TestClass 0x10cf3b80> valueForUndefinedKey:]: this class is not key value coding-compliant for the key testValue. '

NSString* predicateString = [NSString stringWithFormat:@"name CONTAINS[cd] %@",@"testValue"];
NSPredicate* filterPredicate = [NSPredicate predicateWithFormat:predicateString];
NSArray* filtered = [all filteredArrayUsingPredicate:filterPredicate];

名字是一个NSString*我类(class)的属性(property),都是一个数组,里面有我的对象。只是一些微不足道的代码,没有什么额外的。

感谢您的帮助!

最佳答案

如果您将谓词构建为 NSString,它应该是(注意额外的引号):

NSString* predicateString = [NSString stringWithFormat:@"name CONTAINS[cd] '%@'", @"testValue"];

或者,您可以更直接地构建它:

NSPredicate* filterPredicate = [NSPredicate predicateWithFormat:@"name CONTAINS[cd] %@", @"testValue"];

第二种方法使用 %@ 说明符自动将引号括起来。

关于ios - 通过 NSPredicate 过滤数组时出现 NSUnknownKeyException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24183074/

相关文章:

ios - 对字符串数组进行排序并忽略大小写

ios - 调用嵌入式 containerVC 方法时不调用父 VC 委托(delegate)方法

iphone - NSString boundingRectWithSize 使用 CoreText Framesetter 切割高度短? iOS

ios - 组合不同语言 RTL 和 LTR 中的两个字符串

ios - 使用 NSPredicate 搜索字典中的所有行

iOS 10 模拟器不能在代理后面工作

iphone - 在iPhone中创建图像的缩略图?

iphone - 除非点击按钮两次,否则 iOS 网页不会加载

ios - NSPredicate 不断使我的应用程序崩溃

swift - RealmSwift 错误 : "RLMException, reason: Collection was mutated while being enumerated."