Re: NSPredicate for a ANY match based on two properties at once
Re: NSPredicate for a ANY match based on two properties at once
- Subject: Re: NSPredicate for a ANY match based on two properties at once
- From: "Jim Turner" <email@hidden>
- Date: Fri, 20 Jun 2008 15:48:04 -0500
On Fri, Jun 20, 2008 at 2:06 PM, Jesse Grosjean
<email@hidden> wrote:
>>> I want to construct searches such as:
>>>
>>> - find all entries that have a the tag named "priority" with the
>>> value "1".
>
>> NSPredicate *tagPredicate = [NSPredicate predicateWithFormat:@"ANY
>> tags.name = %@", tag.name];
>> NSPredicate *valuePredicate = [NSPredicate predicateWithFormat:@"ANY
>> tags.value = %@", tag.value];
>>
>> NSPredicate *combinedPredicate = [NSComparisonPredicate
>> andPredicateWithSubpredicates:[NSArray arrayWithObjects:tagPredicate,
>> valuePredicate,nil]];
>
> Thanks, but I don't think this quite works. The problem is that the two ANY
> predicates aren't forced to match against the same tag. So if the potential
> matching entry has multiple tags this query will match if any of those tags
> match the tag name, and if any of the tags values match the tag name, but
> there's no constraint that a the same tag value pair matches both
> constraints.
>
> Jesse
>
You are correct, it doesn't work. And in trying to figure this out,
I've come to realize that I've used predicates "the hard way" in some
of my code. Try this instead:
NSPredicate *pred = [NSPredicate predicateWithFormat:@"(SUBQUERY(
tags, $x, $x.name = %@ AND $x.value = %@).@count != 0)",
@"aNameToMatch", @"aValueToMatch"];
Appears that this was a problem in 10.4 and fixed in 10.5.
NSExpression's documentation for
expressionForSubquery:usingIteratorVariable:predicate: has some
discussion about it.
--
Jim
http://nukethemfromorbit.com
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden