PDA

View Full Version : How do we combine two NSPredicate object?




teguh123
Jun 1, 2011, 06:14 AM
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.



Guiyon
Jun 1, 2011, 07:49 AM
I would suggest checking out the NSCompoundPredicate (http://developer.apple.com/library/ios/#DOCUMENTATION/Cocoa/Reference/Foundation/Classes/NSCompoundPredicate_Class/Reference/Reference.html) class.

teguh123
Jun 1, 2011, 09:16 PM
I would suggest checking out the NSCompoundPredicate (http://developer.apple.com/library/ios/#DOCUMENTATION/Cocoa/Reference/Foundation/Classes/NSCompoundPredicate_Class/Reference/Reference.html) class.

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?

hiteshsharma
Jun 1, 2011, 11:27 PM
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?

chown33
Jun 2, 2011, 12:32 AM
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?

Start at the class reference for a specific class, such as NSPredicate:
http://developer.apple.com/library/ios/#DOCUMENTATION/Cocoa/Reference/Foundation/Classes/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.