Reputation: 520
I'm wondering if it's possible to filter an NSArray with a NSPredicate using an array index value, ie
NSArray *completeArray = ...;
NSArray *filteredArray = [completeArray filteredArrayUsingPredicate:[NSPredicate
predicateWithFormat:@"ANY (arrayIndex < 5) && (Other conditions here)"];
I'm using core data, and cannot use a primary key or other identifiers for the objects.
Upvotes: 0
Views: 405
Reputation: 243156
No, not really. You could probably get something really hackish working by using a SUBQUERY
, but even I, who am known to do unconventional things with predicates, think that would be a bad idea.
You say that you're doing this with CoreData. If that's the case, then your request does not make sense, because CoreData does nothing with ordering things. Yes you can specify sort descriptors to a fetch request, but those aren't applied until after the predicate is used. Any data that gets processed by the predicate is inherently unordered. So asking for an "array index value" to use in a predicate for CoreData would not only be an abuse of the predicate system, but represents an inherent misunderstanding about the CoreData framework.
CoreData is unordered. Any ordering you want, you have to apply yourself.
Upvotes: 0
Reputation: 46598
use subarrayWithRange:
then filter the subarray
NSArray *subarray = [completeArray subarrayWithRange:NSMakeRange(0, 5)];
NSArray *filteredArray = [subarray filteredArrayUsingPredicate:[NSPredicate
predicateWithFormat:@"ANY (Other conditions here)"]
Upvotes: 2