1

I have a really annoying bug. May be there is a trivial solution, but I can't get it.

I use NSPredicate often without any problem. Now the following bug comes: Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<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];

name is an NSString* property of my class, all is an array with my objects in it. Just some trivial code, nothing extra.

Thanks for any help!

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Tom
  • 3,899
  • 22
  • 78
  • 137

1 Answers1

1

If you construct the predicate as an NSString it should be (note the extra quotes):

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

Alternatively you can build it more directly with:

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

The second method automatically puts quotes around arguments with the %@ specifier.

Pokey McPokerson
  • 752
  • 6
  • 17