Skip navigation.
 
mlPredicate expressions
FROM : Fabian
DATE : Sat May 31 22:57:56 2008

Hello,

I'm trying to figure out how to use NSExpressions in a predicate but
find the documentation a bit deficient.

Here is what I want to do: I have an array - array A - containing
objects with a "title" and "subtitle" property. I want to filter this
array so it only contains the objects in array B. The example below
demonstrates what I want to achieve, but lacks in precision. It only
checks the presence of the title and subtitle in array B, so the pair
could just as well belong to two different objects. I need to know
that both the title and subtitle of object X in array A matches the
title and subtitle of object Y in array B. Will a NSExpression let me
do this?

NSArray *arrayB = [self getObjects] ;

NSArray *titles = [arrayB arrayByApplyingSelector:@selector(title)];
NSArray *subtitles = [arrayB arrayByApplyingSelector:@selector(subtitle)];
       
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(title IN
%@) AND (subtitle IN %@)", titles, subtitles];
NSArray *result = [arrayA filteredArrayUsingPredicate:predicate];


Regards,
Fabian

Related mailsAuthorDate
mlPredicate expressions Fabian May 31, 22:57
mlRe: Predicate expressions Kyle Sluder Jun 1, 21:26
mlRe: Predicate expressions Hamish Allan Jun 1, 21:35
mlRe: Predicate expressions Hamish Allan Jun 1, 21:40