NSPredicate用法和详解

1. 基本用法

###1.1 比较运算符>,<,==,>=,<=,!= 以下代码所示是比较对象car中属性carName值是否等于BaoMa. 若是是NSInteger类型变量,也能够用于比较大小. 若是是查找一个字符串中子串范围, 须要用正则表达式.正则表达式

若是是比较字符串, 须要这么写SELF == 'i love you'", 若是是比较数字大小:"licheng > 100"数组

NSPredicate *predicate1 = [NSPredicate predicateWithFormat:@"carName == 'BMW'"];
BOOL match = [predicate1 evaluateWithObject:car];
if (match) {
    NSLog(@"match");
}else{
    NSLog(@"not match");
}

1.2 数组筛选

使用方法以下代码所示, 经过filteredArrayUsingPredicate:这个方法筛选出符合条件的每个对象,而后会放到一个新的数组里面, 能够对数组进行相应的处理.lua

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"mileage > 100"];
NSArray *results = [cars filteredArrayUsingPredicate:predicate];
NSLog(@"resules = %@", results);

1.3 字符串自己:SELF

例:@“SELF == ‘APPLE’"code

2. 强大的数组运算符

predicate = [NSPredicate predicateWithFormat:  
                 @"engine.horsepower BETWEEN { 50, 200 }"];  
    results = [cars filteredArrayUsingPredicate: predicate];  
    NSLog (@"%@", results);  
     
    NSArray *betweens = [NSArray arrayWithObjects:  
                         [NSNumber numberWithInt: 50], [NSNumber numberWithInt: 200], nil];  
    predicate = [NSPredicate predicateWithFormat: @"engine.horsepower BETWEEN %@", betweens];  
    results = [cars filteredArrayUsingPredicate: predicate];  
    NSLog (@"%@", results);  
    predicateTemplate = [NSPredicate predicateWithFormat: @"engine.horsepower BETWEEN $POWERS"];  
    varDict = [NSDictionary dictionaryWithObjectsAndKeys: betweens, @"POWERS", nil];  
    predicate = [predicateTemplate predicateWithSubstitutionVariables: varDict];  
    results = [cars filteredArrayUsingPredicate: predicate];  
    NSLog (@"%@", results);  
  
//IN运算符  
    predicate = [NSPredicate predicateWithFormat: @"name IN { 'Herbie', 'Snugs', 'Badger', 'Flap' }"];  
    results = [cars filteredArrayUsingPredicate: predicate];  
    NSLog (@"%@", [results valueForKey: @"name"]);  
    predicate = [NSPredicate predicateWithFormat: @"SELF.name IN { 'Herbie', 'Snugs', 'Badger', 'Flap' }"];  
    results = [cars filteredArrayUsingPredicate: predicate];  
    NSLog (@"%@", [results valueForKey: @"name"]);  
     
    names = [cars valueForKey: @"name"];  
    predicate = [NSPredicate predicateWithFormat: @"SELF IN { 'Herbie', 'Snugs', 'Badger', 'Flap' }"];  
    results = [names filteredArrayUsingPredicate: predicate];//这里限制了SELF的范围  
    NSLog (@"%@", results);  
//BEGINSWITH,ENDSWITH,CONTAINS  
  
//附加符号,[c],[d],[cd],c表示不区分大小写,d表示不区分发音字符,cd表示什么都不区分  
    predicate = [NSPredicate predicateWithFormat: @"name BEGINSWITH 'Bad'"];  
    results = [cars filteredArrayUsingPredicate: predicate];  
    NSLog (@"%@", results);  

    
    predicate = [NSPredicate predicateWithFormat: @"name BEGINSWITH 'HERB'"];  
    results = [cars filteredArrayUsingPredicate: predicate];  
    NSLog (@"%@", results);  
     
    predicate = [NSPredicate predicateWithFormat: @"name BEGINSWITH[cd] 'HERB'"];  
    results = [cars filteredArrayUsingPredicate: predicate];  
    NSLog (@"%@", results);  
  
//LIKE运算符(通配符)  
    predicate = [NSPredicate predicateWithFormat: @"name LIKE[cd] '*er*'"];  
    results = [cars filteredArrayUsingPredicate: predicate];  
    NSLog (@"%@", results);  
     
    predicate = [NSPredicate predicateWithFormat: @"name LIKE[cd] '???er*'"];  
    results = [cars filteredArrayUsingPredicate: predicate];  
    NSLog (@"%@", results);
相关文章
相关标签/搜索