使用NSPredictate导致错误的原因是什么?

时间:2012-01-01 16:02:05

标签: objective-c

我使用了NSPredictate类,但发生了以下错误。 我不知道原因。

为什么会出现以下错误?

enter image description here

关注是源代码。

#import "Predictate.h"

@implementation Predictate

@synthesize dictate;

-(id)init{
    if ((self = [super init])) {
    }
    return self;
}

- (void)Predictate{
    dictate = [[NSMutableArray alloc]initWithObjects:@"AAA",@"BBB",@"CCC", nil];
    NSPredicate *test = [NSPredicate predicateWithFormat:@"dictate like 'AAA'"];

    NSMutableArray *result = [dictate filteredArrayUsingPredicate:test];

    NSLog(@"%@",result);
}

-(void)dealloc{
    [dictate release];
    [super dealloc];
}

@end

错误信息如下。

2012-01-02 00:57:39.972 filter [1750:707] ***由于未捕获的异常'NSUnknownKeyException'终止应用程序,原因:'[< __ NSCFConstantString 0x100002290> valueForUndefinedKey:]:此类不是密钥值编码兼容的密钥指令。'

1 个答案:

答案 0 :(得分:3)

您正在使用NSPredicate过滤字符串对象数组,但正在使用dictate like 'AAA'。谓词绝对不知道这个dictate意味着什么。

您需要将dictate替换为SELF,以便它变为"SELF like 'AAA'"