How do we combine two NSPredicate object?

Discussion in 'iOS Programming' started by teguh123, Jun 1, 2011.

  1. teguh123, Jun 1, 2011
    Last edited by a moderator: Jun 1, 2011

    teguh123 macrumors member

    Mar 22, 2011
    With into one?

    NSPredicate * thePredicate1= [NSPredicate predicateWithFormat: @"Title CONTAINS[cd] %@", searchTerm];
    NSPredicate * thePredicate2= [NSPredicate predicateWithFormat: @"Street CONTAINS[cd] %@", searchTerm];
    NSPredicate * thePredicate3= [NSPredicate predicateWithFormat: @"Building CONTAINS[cd] %@", searchTerm];
    NSPredicate * thePredicate4= [NSPredicate predicateWithFormat: @"any Districts CONTAINS[cd] %@", searchTerm];
    I want thePredicate= Predicate1 or predicate2 or predicate 3 or something like that.
  2. Guiyon, Jun 1, 2011
    Last edited: Jun 1, 2011

    Guiyon macrumors 6502a

    Mar 19, 2008
    North Shore, MA
  3. teguh123 thread starter macrumors member

    Mar 22, 2011
    Awesome, how else I can ask this more quickly by search engine. Searching for combining NSPredicate don't see any result.

    Hmm.... How do we know what soft of subclasses of NSPredicate?
  4. hiteshsharma macrumors newbie

    Jun 1, 2011
    I've read the NSPredicate documentation and couldn't wrap my head around it all the way. Simple predicates I can understand, but now how do I create a predicate for the following situation:

    I have an array of Foo objects. In the Foo class, one of the iVars is an NSMutableArray of Bar objects. I need to filter the array of Foo objects to get those that have a particular Bar object in its array. How?

    Also, is it any easier (or possible) to find multiple Bar objects at once, or to combine several predicates that each search for a particular Bar?
  5. chown33 macrumors 604

    Aug 9, 2009
    Sailing beyond the sunset
    Start at the class reference for a specific class, such as NSPredicate:

    You can use search engine terms: ClassNameHere class reference
    where ClassNameHere is the specific class name you're looking for. However you get there, start at the class reference doc.

    Once in the class reference doc, look at the column on the left, where it says COMPANION GUIDE, under which is "Predicate Programming Guide". Click that:

    Now look on the left again, maybe scrolling it down so you can see the classes under the OBJECTIVE-C REFERENCE heading:

    Those classes are the predicate classes. Click each one to show its class reference doc.

    This is the general strategy:
    1. Look up a particular class reference doc (example: NSPredicate)
    2. Look on the left for COMPANION GUIDE links, there may be more than one (example: Predicate Programming Guide).
    3. After reading the companion guide, look on its left for the relevant classes, usually listed under OBJECTIVE-C REFERENCE, which will be class reference docs.
    4. Be sure to look for RELATED DOCUMENTS (again on the left).

    This is the general structure of all Cocoa documents. The class reference doc has links to companion guides on the left, and companion guides have links to class reference docs and related documents.

Share This Page