How do we combine two NSPredicate object?

Discussion in 'iPhone/iPad Programming' started by teguh123, Jun 1, 2011.

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

    macrumors member

    Joined:
    Mar 22, 2011
    #1
    With into one?

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

    macrumors 6502a

    Joined:
    Mar 19, 2008
    Location:
    North Shore, MA
    #2
  3. thread starter macrumors member

    Joined:
    Mar 22, 2011
    #3
    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. macrumors newbie

    Joined:
    Jun 1, 2011
    #4
    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. macrumors 603

    Joined:
    Aug 9, 2009
    #5
    Start at the class reference for a specific class, such as NSPredicate:
    http://developer.apple.com/library/.../NSPredicate_Class/Reference/NSPredicate.html

    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:
    http://developer.apple.com/library/ios/#DOCUMENTATION/Cocoa/Conceptual/Predicates/predicates.html

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

    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