3

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 Answers 2

11

NSPredicate doesn't use formats like NSString does. So that the result of a predicate creation using the pre-generated predicateString won't be a valid predicate.

You have to make it an actual predicate:

NSPredicate * predicate = [NSPredicate predicateWithFormat:@"identifier IN %@", employeeIDsArray];
[fetchRequest setPredicate:predicate];

See the documentation for more informations on predicate formats.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, but of course I do call: NSPredicate *predicate = [NSPredicate predicateWithFormat:predicateString];
Apologies for not accepting your answer earlier. I have tried your suggestion and it works.
4

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.

2 Comments

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.