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"];
results = [cars filteredArrayUsingPredicate: predicate];
NSLog (@"%@", results);
NSPredicate *predicateTemplate;
NSDictionary *varDict;
predicateTemplate = [NSPredicatepredicateWithFormat: @"engine.horsepower > $POWER"];//创建一个键/值字典来存储,这里$符号用于变量
varDict = [NSDictionarydictionaryWithObjectsAndKeys:
[],@"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:
[], [],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);
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运算符。赋给该运算符一个正则表达式。