首页 > 代码库 > NSPredicate
NSPredicate
Cocoa提供了一个类NSPredicate类,该类主要用于指定过滤器的条件,该对象可以准确的描述所需条件,对每个对象通过谓词进行筛选,判断是否与条件相匹配。谓词表示计算真值或假值的函数。
NSPredicate *predicate;
predicate=[NSPredicatepredicateWithFormat:@"name==‘Herbie‘"];//用predicateWithFormat创建一个谓词,name作为键路径
BOOL match=[predicateevaluateWithObject:car];//car作为接收对象,evaluateWithObject返回一个bool值。
NSLog(@"%s",(match)?"YES":"NO");
predicate = [NSPredicatepredicateWithFormat:@"engine.horsepower > 150"];
NSArray *cars = [garage cars];
for (Car *car in [garage cars]) {
if ([predicate evaluateWithObject: car]) {
NSLog (@"%@", car.name);
}
}
遍历cars判断是否是真,然后输出。
predicate = [NSPredicatepredicateWithFormat:@"engine.horsepower > 150"];
NSArray *results;
results = [carsfilteredArrayUsingPredicate: predicate];//它将循环过滤数组内容,根据谓词计算每个对象的值,并将值为YES的对象累计到将被返回的新数组中。
NSLog (@"%@", results);
filteredArrayUsingPredicate这个方法可以得到我们所需要的值。加上
NSArray *names;
names=[results valueForKey:@"name"];
NSLog (@"%@", names);
就和上面的值一样。
NSMutableArray *carsCopy = [carsmutableCopy];
[carsCopyfilterUsingPredicate: predicate];//filterUsingPredicate和NSMutableArray构成新数组。
NSLog (@"%@", carsCopy);
predicate = [NSPredicatepredicateWithFormat:@"engine.horsepower > %d", 50];
results = [cars filteredArrayUsingPredicate: predicate];
NSLog (@"%@", results);
NSPredicate *predicateTemplate;
NSDictionary *varDict;
predicateTemplate = [NSPredicatepredicateWithFormat: @"engine.horsepower > $POWER"];//创建一个键/值字典来存储,这里$符号用于变量
varDict = [NSDictionarydictionaryWithObjectsAndKeys:
[NSNumbernumberWithInt: 150],@"POWER", nil];//@后面参数名
predicate = [predicateTemplatepredicateWithSubstitutionVariables: varDict];
results = [cars filteredArrayUsingPredicate: predicate];
NSLog (@"%@", results);
predicate = [NSPredicatepredicateWithFormat:
@"(engine.horsepower > 50) AND (engine.horsepower < 200)"];//可以运用运算符
results = [cars filteredArrayUsingPredicate: predicate];
NSLog (@"oop %@", results);
predicate = [NSPredicatepredicateWithFormat:
@"engine.horsepower BETWEEN { 50, 200 }"];//这里可以运用between关键字。和上面效果一样
results = [cars filteredArrayUsingPredicate: predicate];
NSLog (@"%@", results);
NSArray *betweens = [NSArray arrayWithObjects:
[NSNumbernumberWithInt: 50], [NSNumbernumberWithInt: 200],nil];
predicate = [NSPredicatepredicateWithFormat: @"engine.horsepower BETWEEN %@", betweens];
results = [cars filteredArrayUsingPredicate: predicate];
NSLog (@"%@", results);
predicateTemplate = [NSPredicatepredicateWithFormat:@"engine.horsepower BETWEEN $POWERS"];
varDict = [NSDictionarydictionaryWithObjectsAndKeys: betweens, @"POWERS", nil];
predicate = [predicateTemplatepredicateWithSubstitutionVariables: varDict];//用变量
results = [cars filteredArrayUsingPredicate: predicate];
NSLog (@"%@", results);
predicate = [NSPredicatepredicateWithFormat:@"name < ‘Newton‘"];//不等号不仅可以用于数字,也可以用于字符串值。
results = [cars filteredArrayUsingPredicate: predicate];
NSLog (@"%@", [results valueForKey: @"name"]);
predicate = [NSPredicatepredicateWithFormat:@"name IN { ‘Herbie‘, ‘Snugs‘, ‘Badger‘, ‘Flap‘ }"];//包含在其中用IN
results = [cars filteredArrayUsingPredicate: predicate];
NSLog (@"%@", [results valueForKey: @"name"]);
predicate = [NSPredicatepredicateWithFormat:@"SELF.name IN { ‘Herbie‘, ‘Snugs‘, ‘Badger‘, ‘Flap‘ }"];//self关键字
results = [cars filteredArrayUsingPredicate: predicate];
NSLog (@"%@", [results valueForKey: @"name"]);
names = [cars valueForKey: @"name"];
predicate = [NSPredicatepredicateWithFormat:@"SELF IN { ‘Herbie‘, ‘Snugs‘, ‘Badger‘, ‘Flap‘ }"];//self关键字
results = [namesfilteredArrayUsingPredicate: predicate];
NSLog (@"%@", results);
predicate = [NSPredicatepredicateWithFormat:@"name BEGINSWITH ‘Bad‘"];//字符串运算符BEGINSWITH,ENDSWITH,CONTAINS[c][d][cd]
results = [cars filteredArrayUsingPredicate: predicate];
NSLog (@"%@", results);
BEGINSWITH:检查某个字符串是否以另一个字符串开头。
ENDSWITH:检查某个字符串是否以另一个字符串结尾。
CONTAINS:检查某个字符串是否以另一个字符串内部。
[c]不区分大小写[d]不区分发音符号即没有重音符号[cd]既不区分大小写,又不区分发音符号。
predicate = [NSPredicatepredicateWithFormat:@"name LIKE[cd] ‘*er*‘"];//*代表通配符Like还接受[cd].
results = [cars filteredArrayUsingPredicate: predicate];
NSLog (@"%@", results);
predicate = [NSPredicatepredicateWithFormat:@"name LIKE[cd] ‘???er*‘"];//?只匹配一个字符并且还可以接受[cd].
results = [cars filteredArrayUsingPredicate: predicate];
NSLog (@"%@", results);
还可以使用MATCHES运算符。赋给该运算符一个正则表达式。