我有一个非常烦人的错误。可能有一个简单的解决方案,但我无法得到它。
我使用 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/