Friday, June 14, 2013

NSComparisonPredicate NSGreaterThanPredicateOperatorType example in Objective C (iOS).


NSComparisonPredicate NSGreaterThanPredicateOperatorType

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 NSGreaterThanPredicateOperatorType example.
// First part: name != <varName>
NSPredicate *p1 = [NSPredicate predicateWithFormat:@"name != %@", varName];

// Second part: date <op> <varDate>
NSExpression *lexp = [NSExpression expressionForKeyPath:@"date"];
NSExpression *rexp = [NSExpression expressionForConstantValue:varDate];
NSPredicateOperatorType op = (direction == kPageDirectionForward) ? NSGreaterThanPredicateOperatorType : NSLessThanOrEqualToPredicateOperatorType;
NSPredicate *p2 = [NSComparisonPredicate predicateWithLeftExpression:lexp
                                                     rightExpression:rexp
                                                            modifier:0
                                                                type:op
                                                             options:0];
// predicate = <p1> AND <p2>
NSPredicate *predicate = [NSCompoundPredicate andPredicateWithSubpredicates:@[p1, p2]];

Example of [NSComparisonPredicate NSGreaterThanPredicateOperatorType].
    //Only events newer than 14 days
    NSDate* breakDate = [NSDate dateWithTimeIntervalSinceNow:(-1)*(14*24*60*60)];
    NSPredicate *p = [NSComparisonPredicate predicateWithLeftExpression:[NSExpression expressionForKeyPath:@"startDate"]
                                                        rightExpression:[NSExpression expressionForConstantValue:breakDate]
                                                               modifier:NSDirectPredicateModifier
                                                                   type:NSGreaterThanPredicateOperatorType
                                                                options:0];
    [fetchRequest setPredicate:p];

End of NSComparisonPredicate NSGreaterThanPredicateOperatorType example article.