Friday, June 14, 2013

NSComparisonPredicate NSInPredicateOperatorType example in Objective C (iOS).


NSComparisonPredicate NSInPredicateOperatorType

NSPredicateOperatorType
Defines the type of comparison for NSComparisonPredicate.

enum {
NSLessThanPredicateOperatorType = 0,
NSLessThanOrEqualToPredicateOperatorType,
NSGreaterThanPredicateOperatorType,
NSGreaterThanOrEqualToPredicateOperatorType,
NSEqualToPredicateOperatorType,
NSNotEqualToPredicateOperatorType,
NSMatchesPredicateOperatorType,
NSLikePredicateOperatorType,
NSBeginsWithPredicateOperatorType,
NSEndsWithPredicateOperatorType,
NSInPredicateOperatorType,
NSCustomSelectorPredicateOperatorType,
NSContainsPredicateOperatorType,
NSBetweenPredicateOperatorType
};
typedef NSUInteger NSPredicateOperatorType;

Constants
NSLessThanPredicateOperatorType
A less-than predicate.
NSLessThanOrEqualToPredicateOperatorType
A less-than-or-equal-to predicate.
NSGreaterThanPredicateOperatorType
A greater-than predicate.
NSGreaterThanOrEqualToPredicateOperatorType
A greater-than-or-equal-to predicate.
NSEqualToPredicateOperatorType
An equal-to predicate.
NSNotEqualToPredicateOperatorType
A not-equal-to predicate.
NSMatchesPredicateOperatorType
A full regular expression matching predicate.
NSLikePredicateOperatorType
A simple subset of the MATCHES predicate, similar in behavior to SQL LIKE.
NSBeginsWithPredicateOperatorType
A begins-with predicate.
NSEndsWithPredicateOperatorType
An ends-with predicate.
NSInPredicateOperatorType
A predicate to determine if the left hand side is in the right hand side.
For strings, returns YES if the left hand side is a substring of the right hand side . For collections, returns YES if the left hand side is in the right hand side .
NSCustomSelectorPredicateOperatorType
A predicate that uses a custom selector that takes a single argument and returns a BOOL value.
The selector is invoked on the left hand side with the right hand side as the argument.
NSContainsPredicateOperatorType
A predicate to determine if the left hand side contains the right hand side.
Returns YES if [lhs contains rhs]; the left hand side must be an NSExpression object that evaluates to a collection
NSBetweenPredicateOperatorType
A predicate to determine if the right hand side lies at or between bounds specified by the left hand side.
Returns YES if [lhs between rhs]; the right hand side must be an array in which the first element sets the lower bound and the second element the upper, inclusive. Comparison is performed using compare: or the class-appropriate equivalent.

NSComparisonPredicate NSInPredicateOperatorType example.
First, I get all the EntityC that satisfy the condition EntityC.name equal to 'SomeName'

NSPredicate *p = [NSPredicate predicateWithFormat:@"name like %@", @"SomeName];
...

NSArray *res = [managedObjectContext executeFetchRequest:fetchRequest error:&error];
Then I get an array of EntityB from above query

NSArray *parentBs = [res valueForKeyPath:@"@distinctUnionOfObjects.parent"];
Than get array of EntityB that satisfy the condition EntityB.EntitiesC.name equal to 'SomeName':

NSExpression *leftExpression = [NSExpression expressionForEvaluatedObject];
NSExpression *rightExpression = [NSExpression expressionForConstantValue:parentBs];

NSPredicate *p = [NSComparisonPredicate predicateWithLeftExpression:leftExpression rightExpression: rightExpression modifier:NSDirectPredicateModifier type:NSInPredicateOperatorType options:0];

Example of [NSComparisonPredicate NSInPredicateOperatorType].
fetchRequest.entity = [NSEntityDescription entityForName:@"myRecord" inManagedObjectContext:self.managedObjectContext]];

NSArray *shipTypes = [NSArray arrayWithObjects:[NSNumber numberWithInt:70],
                                        [NSNumber numberWithInt:71],
                                        [NSNumber numberWithInt:72],
                                        [NSNumber numberWithInt:73],
                                        [NSNumber numberWithInt:74],
                                         nil];
fetchRequest.predicate = [NSComparisonPredicate predicateWithLeftExpression:[NSExpression expressionForKeyPath:@"type"] rightExpression:[NSExpression expressionForConstantValue:shipTypes] modifier:NSDirectPredicateModifier type:NSInPredicateOperatorType options:0];

theRecords = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];

End of NSComparisonPredicate NSInPredicateOperatorType example article.