开发者

Can you use an array index value as a parametre in a predicate to filter an array

开发者 https://www.devze.com 2023-03-09 17:35 出处:网络
I\'m wondering if it\'s possible to filter an NSArray with a NSPredicate using an array index value, ie

I'm wondering if it's possible to filter an NSArray with a NSPredicate using an array index value, ie

NSArray *completeArray = ...;
NSArray *filteredArray = [co开发者_如何学运维mpleteArray 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.


use subarrayWithRange: then filter the subarray

NSArray *subarray = [completeArray subarrayWithRange:NSMakeRange(0, 5)];
NSArray *filteredArray = [subarray filteredArrayUsingPredicate:[NSPredicate 
   predicateWithFormat:@"ANY (Other conditions here)"]


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.

0

精彩评论

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