views:

301

answers:

2

Hello, I've got a short question. I have an NSArray filled with Cars (inherits from NSObject) Car has the @property NSString *engine (also regarded @synthesize ...)

Now I want tu filter the array using NSPredicate

predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"(engine like %@)", searchText]];
newArray = [ArrayWithCars filteredArrayUsingPredicate:predicate];

This throws an valueForUndefinedKey error. Is the predicateWithFormat correct?

thanks for your responses

+1  A: 

First, your code is more verbose than necessary, which always opens you up to the possibility that it's wrong. Try:

predicate = [NSPredicate predicateWithFormat:@"engine like %@", searchText];

Second, "ArrayWithCars" looks like a class name (by convention, classes begin with upper-case). Is it actually a class or an improperly-named local variable (ex: "arrayWithCars" or just "cars")?

Third, what is the exact error? What key is undefined? Don't paraphrase errors when asking others for help - we can't see what you're seeing unless you show us.

Joshua Nozzi
+1  A: 

k, I found the mistake.

predicate = [NSPredicate predicateWithFormat:@"engine like *%@*", searchText];

works correct. The ** were missing. Additionally your searchText should be uppercase.

@Josuhua this is no real code, just to visualize my problem

bend0r
bend0r: Don't case-fold strings; when you need to compare strings case-insensitively, use case-insensitive comparison. In this case, that's `LIKE[c]`.
Peter Hosey
Posting code other than that which you're using usually doesn't help others to help you.
Joshua Nozzi