S.J. Lim
S.J. Lim

Reputation: 3165

How to use key-paths in NSPredicate on an NSArray of NSStrings?

Does anyone know how to use key-paths in NSPredicate?

I'm trying to use key-paths, but an error occurred.
I want to use "dictate like 'AAA'" as key-path.

Source code:

- (void)Predictate{
    dictate = [[NSMutableArray alloc]initWithObjects:@"AAA",@"BBB",@"CCC", nil];

    NSPredicate *test = [NSPredicate predicateWithFormat:@"dictate like 'AAA'"];

    NSMutableArray *result = [dictate filteredArrayUsingPredicate:test];

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

This is the error message:

2012-01-02 01:33:33.673 filter[1867:707] * Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<__NSCFConstantString 0x1000022f0> valueForUndefinedKey:]: this class is not key value coding-compliant for the key dictate.'

Upvotes: 1

Views: 604

Answers (1)

tia
tia

Reputation: 9708

"self LIKE 'AAA'" works. Tested.

It's kind of subtle fact that you can send self to Objective-C object and get the pointer to the object itself (try [obj performselector:@selector(self)]). So that's how it works.

Upvotes: 3

Related Questions