Friday, June 14, 2013

NSComparisonPredicate NSCaseInsensitivePredicateOption example in Objective C (iOS).


NSComparisonPredicate NSCaseInsensitivePredicateOption

NSComparisonPredicate Options
These constants describe the possible types of string comparison for NSComparisonPredicate. These options are supported for LIKE as well as all of the equality/comparison operators.

enum {
NSCaseInsensitivePredicateOption = 0x01,
NSDiacriticInsensitivePredicateOption = 0x02,
NSNormalizedPredicateOption = 0x04,
NSLocaleSensitivePredicateOption = 0x08
};
typedef NSUInteger NSComparisonPredicateOptions;

Constants
NSCaseInsensitivePredicateOption
A case-insensitive predicate.
You represent this option in a predicate format string using a [c] following a string operation (for example, "NeXT" like[c] "next").
NSDiacriticInsensitivePredicateOption
A diacritic-insensitive predicate.
You represent this option in a predicate format string using a [d] following a string operation (for example, "naïve" like[d] "naive").
NSNormalizedPredicateOption
Indicates that the strings to be compared have been preprocessed.
This option supersedes NSCaseInsensitivePredicateOption and NSDiacriticInsensitivePredicateOption, and is intended as a performance optimization option.
You represent this option in a predicate format string using a [n] following a string operation (for example, "WXYZlan" matches[n] ".lan").
NSLocaleSensitivePredicateOption
Indicates that strings to be compared using <, <=, =, =>, > should be handled in a locale-aware fashion.
You represent this option in a predicate format string using a [l] following one of the <, <=, =, =>, > operators (for example, "straße" >[l] "strasse").

NSComparisonPredicate NSCaseInsensitivePredicateOption example.
-(NSPredicate *) predicateWithStudyID:(NSString *) aStudyID studyName:(NSString *) aStudyName date:(NSDate *) aDate{
  NSPredicate *p;
  NSMutableArray *preds=[[NSMutableArray alloc] initWithCapacity:1];
  if (aDate != nil) {
    NSExpression *dateKeyEx=[NSExpression expressionForKeyPath:@"studyDate"];
    NSExpression *aDateEx=[NSExpression expressionForConstantValue:aDate];
    NSPredicate *datePred=[NSComparisonPredicate predicateWithLeftExpression:dateKeyEx
                                                             rightExpression:aDateEx
                                                                    modifier:NSDirectPredicateModifier
                                                                        type:NSEqualToPredicateOperatorType
                                                                     options:0];
    [preds addObject:datePred];
  }
  if (![aStudyID isEqualToString:@""]) {
    NSExpression *studyIDKeyEx=[NSExpression expressionForKeyPath:@"studyID"];
    NSExpression *aStudyIDEx=[NSExpression expressionForConstantValue:aStudyID];
    NSPredicate *studyIDPred=[NSComparisonPredicate predicateWithLeftExpression:studyIDKeyEx
                                                                rightExpression:aStudyIDEx
                                                                       modifier:NSDirectPredicateModifier
                                                                           type:NSLikePredicateOperatorType
                                                                        options:NSCaseInsensitivePredicateOption];
    [preds addObject:studyIDPred];
  }
  if (![aStudyName isEqualToString:@""]) {
    NSExpression *studyNameKeyEx=[NSExpression expressionForKeyPath:@"studyName"];
    NSExpression *aStudyNameEx=[NSExpression expressionForConstantValue:aStudyName];
    NSPredicate *studyNamePred=[NSComparisonPredicate predicateWithLeftExpression:studyNameKeyEx
                                                                rightExpression:aStudyNameEx
                                                                       modifier:NSDirectPredicateModifier
                                                                           type:NSLikePredicateOperatorType
                                                                        options:NSCaseInsensitivePredicateOption];
    [preds addObject:studyNamePred];
  }
  p=[NSCompoundPredicate andPredicateWithSubpredicates:preds]; 
  [preds release];
  return p;
}

Example of [NSComparisonPredicate NSCaseInsensitivePredicateOption].
NSExpression *valueExp = [NSExpression expressionForKeyPath:@"clients.auftraggeber"];
NSExpression *keyExp = [NSExpression expressionForConstantValue:@"Henke"];
NSPredicate *perdicate = [NSComparisonPredicate predicateWithLeftExpression:valueExp
rightExpression:keyExp
modifier:NSDirectPredicateModifier
type:NSLikePredicateOperatorType
options:(NSCaseInsensitivePredicateOption | NSDiacriticInsensitivePredicateOption)];
NSLog([perdicate description]);
NSArray *tmp = [projektArray filteredArrayUsingPredicate:perdicate];

NSComparisonPredicate NSCaseInsensitivePredicateOption example.
NSPredicate *nameQuery =
[NSComparisonPredicate predicateWithLeftExpression:[NSExpression expressionForKeyPath:@"s_name"]
                                   rightExpression:[NSExpression expressionForConstantValue:Name]
                                          modifier:NSDirectPredicateModifier
                                              type:NSLikePredicateOperatorType
                                           options:NSCaseInsensitivePredicateOption|NSDiacriticInsensitivePredicateOption];

NSPredicate *regNoQuery =
[NSComparisonPredicate predicateWithLeftExpression:[NSExpression expressionForKeyPath:@"s_regno"]
                                   rightExpression:[NSExpression expressionForConstantValue:RegNo]
                                          modifier:NSDirectPredicateModifier
                                              type:NSEqualToPredicateOperatorType
                                           options:0];

NSPredicate *query = [NSCompoundPredicate andPredicateWithSubpredicates:@[nameQuery,regNoQuery]];

End of NSComparisonPredicate NSCaseInsensitivePredicateOption example article.