NSPredicate可用于检索查询,是一个过滤容器,相当于数据库的where
基本用法
基本查询
注意: 如果谓词中的文本块没有被引用(单引号或双引号),则被当做是键路径,否则认为是字符串
//基本的查询
NSPredicate *predicate;
//方法一:
predicate = [NSPredicate predicateWithFormat:@"name == 'Herbie'"];
//方法二:
predicate = [NSPredicate predicateWithFormat:@"name == %@", @"Herbie"];
//方法三:%K表示key
predicate = [NSPredicate predicateWithFormat:@"%K == %@", @"name", @"Herbie"];
BOOL match = [predicate evaluateWithObject:car];
NSLog(@"%s", (match) ? "YES" : "NO");
//方法四:含有变量的谓词,注意不能使用$VARIABLE作为路径名,因为它值代表值,用predicateWithSubstitutionVariables调用来构造新的谓词(键/值字典),其中键是变量名,值是要插入的内容,注意这种情况下不能把变量当成键路径,只能用作值
NSPredicate *predicateTemplate = [NSPredicate predicateWithFormat:@"name == $NAME"];
NSDictionary *varDict = [NSDictionary dictionaryWithObjectsAndKeys:@"Herbie", @"NAME", nil];
predicate = [predicateTemplate predicateWithSubstitutionVariables:varDict];
NSLog(@"predicate:==========%@", predicate);
match = [predicate evaluateWithObject:self.car];
NSLog (@"%s", (match) ? "YES" : "NO");
数组过滤匹配
集合(set)过滤:filteredSetUsingPredicate:
//输出完整的信息,在整个数组里寻找匹配的结果
predicate = [NSPredicate predicateWithFormat:@"engine.horsepower > 150"];
NSArray *results = [self.cars filteredArrayUsingPredicate: predicate];
NSLog (@"%@", results);
运算符
比较和逻辑运算符
符号 | 意义 |
---|---|
== | 等于 |
> | 大于 |
>= (=>) | 大于或等于 |
< | 小于 |
<= (=<) | 小于或等于 |
!= (<>) | 不等于 |
括号和逻辑运算AND、OR、NOT或者C样式的等效表达式&&、||、!
注意:不等号适用于数字和字符串
//输出完整的信息,在整个数组里寻找匹配的结果
predicate = [NSPredicate predicateWithFormat:@"engine.horsepower > 150"];
NSArray *results = [self.cars filteredArrayUsingPredicate: predicate];
NSLog (@"%@", results);
//谓词字符窜还支持C语言中一些常用的运算符
predicate = [NSPredicate predicateWithFormat:@"(engine.horsepower > 50) AND (engine.horsepower < 200)"];
results = [self.cars filteredArrayUsingPredicate: predicate];
NSLog (@"C语言中一些常用的运算符~~~~~~~%@", results);
//比较字符串的大小
predicate = [NSPredicate predicateWithFormat:@"name < 'Newton'"];
results = [self.cars filteredArrayUsingPredicate: predicate];
NSLog (@"%@", [results valueForKey: @"name"]);
数组运算符
BETWEEN和IN后加某个数组
//BETWEEN运算符
//方法一:
predicate = [NSPredicate predicateWithFormat:@"engine.horsepower BETWEEN {50, 200}"];
results = [self.cars filteredArrayUsingPredicate: predicate];
//获得result里对象的name的属性的集合
NSArray *names = [results valueForKey:@"name"];
NSLog (@"~~~~~~%@~~~~~~~~%@", results, names);
//方法二:
NSArray *betweens = [NSArray arrayWithObjects:[NSNumber numberWithInt: 50], [NSNumber numberWithInt: 200], nil];
predicate = [NSPredicate predicateWithFormat:@"engine.horsepower BETWEEN %@", betweens];
results = [self.cars filteredArrayUsingPredicate: predicate];
NSLog (@"%@", results);
//方法三:
predicateTemplate = [NSPredicate predicateWithFormat:@"engine.horsepower BETWEEN $POWERS"];
varDict = [NSDictionary dictionaryWithObjectsAndKeys:betweens, @"POWERS", nil];
predicate = [predicateTemplate predicateWithSubstitutionVariables:varDict];
results = [self.cars filteredArrayUsingPredicate:predicate];
NSLog (@"%@", results);
//IN运算符
//方法一:
predicate = [NSPredicate predicateWithFormat:@"name IN {'Herbie', 'Snugs', 'Badger', 'Flap'}"];
results = [self.cars filteredArrayUsingPredicate:predicate];
NSLog (@"%@", [results valueForKey:@"name"]);
//方法二:SELF表示自己
predicate = [NSPredicate predicateWithFormat:@"SELF.name IN { 'Herbie', 'Snugs', 'Badger', 'Flap'}"];
results = [self.cars filteredArrayUsingPredicate: predicate];
NSLog (@"%@", [results valueForKey: @"name"]);
实例:多个稿子articleType过滤
//==========去重articleType不在范围内的的稿子
NSPredicate *artPredicate = [NSPredicate predicateWithFormat:@"articleType IN {'1', '2', '3', '10'}"];
NSMutableArray *artTempArray = [NSMutableArray arrayWithArray:[tempArray filteredArrayUsingPredicate:artPredicate]];
字符串运算符
BEGINSWITH:以某个字符串开头
ENDSWITH:以某个字符串结束
CONTAINS:包含某个字符串
附加符号:[c] [d] [cd] c表示不区分大小写,d表示不区分发音字符,cd表示什么都不区分
predicate = [NSPredicate predicateWithFormat:@"name BEGINSWITH 'Bad'"];
results = [self.cars filteredArrayUsingPredicate: predicate];
NSLog (@"%@", results);
predicate = [NSPredicate predicateWithFormat:@"name BEGINSWITH 'HERB'"];
results = [self.cars filteredArrayUsingPredicate: predicate];
NSLog (@"%@", results);
predicate = [NSPredicate predicateWithFormat:@"name BEGINSWITH[cd] 'HERB'"];
results = [self.cars filteredArrayUsingPredicate: predicate];
NSLog (@"%@", results);
LIKE和MATCHES运算符
LIKE:与通配符 "*" 和 "?"结合使用,也接受[cd]符号
"*":表示任意多个字符匹配
"?":表示一个字符匹配
MATCHES:可以使用正则表达式
self.car.name = @"hhher";
//LIKE运算符(通配符)
predicate = [NSPredicate predicateWithFormat:@"name LIKE[cd] '*er*'"];
match = [predicate evaluateWithObject:self.car];
NSLog (@"%s", (match) ? "YES" : "NO");
predicate = [NSPredicate predicateWithFormat:@"name LIKE[cd] '???er*'"];
match = [predicate evaluateWithObject:self.car];
NSLog (@"%s", (match) ? "YES" : "NO");
合计操作
ANY,SOME:指定下列表达式中的任意元素。比如,ANY children.age < 18。
ALL:指定下列表达式中的所有元素。比如,ALL children.age < 18。
NONE:指定下列表达式中没有的元素。比如,NONE children.age < 18。它在逻辑上等于NOT (ANY ...)。
IN:等于SQL的IN操作,左边的表达必须出现在右边指定的集合中。比如,name IN {'Ben', 'Melissa', 'Nick'}。
复合谓词NSCompoundPredicate
创建复合谓词
[NSCompoundPredicate andPredicateWithSubpredicates:@[[NSPredicate predicateWithFormat:@"age > 25"], [NSPredicate predicateWithFormat:@"firstName = %@", @"Quentin"]]];
[NSPredicate predicateWithFormat:@"(age > 25) AND (firstName = %@)", @"Quentin"];
参考链接
http://justsee.iteye.com/blog/1816971
http://nshipster.cn/nspredicate/