Reputation: 34798
How can I perform an NSArray filteredArrayUsingPredicate where the predicate is a method? That is what would a simple code example look like here?
I've been trying to go through the predicate doco and getting a little confused. I can see how it works for simple checks, but if I have a check which requires a few lines of objective-c code to implement what would the code look like to effectively:
thanks
Upvotes: 4
Views: 4311
Reputation: 243156
You can use the predicateWithBlock:
approach that @lxt suggested, or you can use the FUNCTION
approach. This would have you build a predicate that looks like this:
[NSPredicate predicateWithFormat:@"FUNCTION(SELF, 'mySuperMethod:', %@)", aParameter];
If you use that predicate to filter an array, then:
SELF
will iteratively be each item in the array-mySuperMethod:
method invoked-mySuperMethod:
will receive aParameter
as the parameter to the method-mySuperMethod:
will return a BOOL
that has been boxed in an NSNumber
<< this is very importantYES
from -mySuperMethod:
will be included in the filtered array.For more information on this syntax, check out this blog post.
So why might you want to use this approach over the block approach? I can think of two reasons:
aParameter
conforms to the <NSCoding>
protocol. Blocks cannot be serialized.However, if neither of those are requirements, then the block approach will probably be better in the long run, since it's more obvious and readable. :)
Upvotes: 6
Reputation: 31304
As long as you're going with iOS 4.0 and above you'll be glad to know this is really straightforward (the following isn't available on 3.x).
You can use the predicateWithBlock
method to create a NSPredicate that takes a block that returns YES or NO as its argument. So pretty much exactly what you want (if you're not familiar with blocks they're basically a way to encapsulate a method. See here: http://pragmaticstudio.com/blog/2010/7/28/ios4-blocks-1)
+ (NSPredicate *)predicateWithBlock:(BOOL (^)(id evaluatedObject, NSDictionary *bindings))block
Upvotes: 7