• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: NSPredicate for a ANY match based on two properties at once
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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 13:06:57 -0500

On Fri, Jun 20, 2008 at 10:56 AM, Jesse Grosjean
<email@hidden> wrote:
> I'm having a hard time figuring out how to construct a query. The objects
> involved are:
>
> Entry
>        - has many tags
>
> Tag
>        - name (string)
>        - value (string)
>
> I want to construct searches such as:
>
>        - find all entries that have a the tag named "priority" with the
> value "1".
>
> I can use [NSPredicate predicateWithFormat:@"ANY tags.name = %@", tag.name]
> to find all entries that have a particular tag... but I don't know how to
> also figure in the value.
>
> I don't need this predicate to work with a database, these queries are all
> just being run in memory, with my own custom model objects (not using Core
> Data). If there is no way to do this in a format string, is there some way
> to do it by directly constructing a predicate object?
>
> Thanks,
> Jesse
> _______________________________________________
>

Jesse,

I'm thinking NSCompoundPredicate is what you're looking for.  You
should be able to do something like this:

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]];

If the "simple" predicateWithFormat: predicates don't work,
NSComparisonPredicates will definitely do what you need (I've never
tried creating a compound predicate with predicateWithFormat:
predicates)

--
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

  • Follow-Ups:
    • Re: NSPredicate for a ANY match based on two properties at once
      • From: Jesse Grosjean <email@hidden>
References: 
 >NSPredicate for a ANY match based on two properties at once (From: Jesse Grosjean <email@hidden>)

  • Prev by Date: Re: hitTest question
  • Next by Date: OT: Announcing Pi, the Phoenix iPhone Developer Group
  • Previous by thread: NSPredicate for a ANY match based on two properties at once
  • Next by thread: Re: NSPredicate for a ANY match based on two properties at once
  • Index(es):
    • Date
    • Thread