views:

165

answers:

2

I am trying to set a fetch request with a predicate to obtain records in the store whose identifiers attribute match an array of identifiers specified in the predicate e.g.

NSString *predicateString = [NSString stringWithFormat:@"identifier IN %@", employeeIDsArray];

The employeeIDsArray contains a number of NSNumber objects that match IDs in the store. However, I get an error "Unable to parse the format string". This type of predicate works if it is used for filtering an array, but as mentioned, fails for a core data fetch. How should I set the predicate please?

+2  A: 

You have to make it an actual predicate:

NSPredicate * predicate = [NSPredicate predicateWithFormat:@"identifier IN %@", employeeIDsArray];
[fetchRequest setPredicate:predicate];
Dave DeLong
Thanks, but of course I do call: NSPredicate *predicate = [NSPredicate predicateWithFormat:predicateString];
Run Loop
Apologies for not accepting your answer earlier. I have tried your suggestion and it works.
Run Loop
+2  A: 

When you create that string using stringWithFormat:, that method inserts the array's description (a string describing the contents of the array) where you had %@.

That's not what you want. You don't want to test membership in a string describing the array; you want to test membership in the array. So, don't go through stringWithFormat:—pass the format string and the array to predicateWithFormat: directly.

Peter Hosey
Thank you. Had to give "Answered" to the first poster, but +1 because thanks to you I now understand why.
Run Loop
+1 for noticing why it wasn't working! I just thought he had forgotten to turn his string into a predicate.
Dave DeLong