开发者

Core Data fetch request with array

I am trying to set a fetch request with a predicate to obtain records in the store whose identifiers attribute开发者_如何学Go 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?


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜