NSPredicate 的简单介绍
来源:互联网 发布:淘宝网上怎样申请退款 编辑:程序博客网 时间:2024/04/30 08:01
NSPredicate 的简单介绍
/*
本工程主要介绍谓词的使用
// 参照 http://www.cocoachina.com/ios/20160111/14926.html
http://blog.csdn.net/a380880304/article/details/49022179
*/
import “ViewController.h”
import “person.h”
@interface ViewController ()
@end
@implementation ViewController- (void)viewDidLoad { [superviewDidLoad];// [self test1];// [self test2];// [self test3]; // [self test4]; [selftest5]; // [self test6];}#pragma mark 组合谓词-(void)test6{ NSArray *ary =@[@8,@18,@28,@38,@48,@58,@68]; NSPredicate *predicate = [NSPredicatepredicateWithFormat:@"SELF > 18"]; NSPredicate *predicate1 = [NSPredicatepredicateWithFormat:@"SELF < 58"]; // 将 predicate和 predicate1 组合使用 NSCompoundPredicate *compoundPredicate = [NSCompoundPredicateandPredicateWithSubpredicates:@[predicate1,predicate]]; NSArray *result = [aryfilteredArrayUsingPredicate:compoundPredicate]; NSLog(@"%@", result); // 打印结果 28, 38, 48 // [NSPredicate predicatewith] /* NSLessThanPredicateOperatorType = 0, // compare: returns NSOrderedAscending NSLessThanOrEqualToPredicateOperatorType, // compare: returns NSOrderedAscending || NSOrderedSame NSGreaterThanPredicateOperatorType, // compare: returns NSOrderedDescending NSGreaterThanOrEqualToPredicateOperatorType, // compare: returns NSOrderedDescending || NSOrderedSame NSEqualToPredicateOperatorType, // isEqual: returns true NSNotEqualToPredicateOperatorType, // isEqual: returns false NSMatchesPredicateOperatorType, NSLikePredicateOperatorType, NSBeginsWithPredicateOperatorType, NSEndsWithPredicateOperatorType, NSInPredicateOperatorType, // rhs contains lhs returns true NSCustomSelectorPredicateOperatorType, */ /* NSCaseInsensitivePredicateOption = 0x01, NSDiacriticInsensitivePredicateOption = 0x02, */ NSArray *nameAry =@[@"张三",@"李四" ,@"宋五",@"马留",@"张三",@"李四"]; NSArray *ageAry =@[@8, @18,@28 , @38 ,@48, @58]; NSMutableArray *mary = [NSMutableArrayarray]; for (int i =0; i < nameAry.count; i++) { person *per = [personnew]; per.name = nameAry[i]; per.age = ageAry[i]; [mary addObject:per]; } NSMutableArray *searchItemsPredicate = [NSMutableArrayarray]; NSExpression *lhs = [NSExpressionexpressionForKeyPath:@"name"]; NSExpression *rhs = [NSExpressionexpressionForConstantValue:@"张三"]; NSPredicate *finalPredicate = [NSComparisonPredicate predicateWithLeftExpression:lhs rightExpression:rhs modifier:NSDirectPredicateModifier type:NSContainsPredicateOperatorType options:NSCaseInsensitivePredicateOption]; [searchItemsPredicate addObject:finalPredicate]; NSExpression *lhs1 = [NSExpressionexpressionForKeyPath:@"age"]; NSExpression *rhs1 = [NSExpressionexpressionForConstantValue:@"25"]; // NSCaseInsensitivePredicateOption 不区分大小写 相当于 [c], // NSDiacriticInsensitivePredicateOption忽略发音符号 相对于 [d] // NSNormalizedPredicateOption 表示待比较的字符串已经被预处理了. NSPredicate *finalPredicate1 = [NSComparisonPredicate predicateWithLeftExpression:lhs1 rightExpression:rhs1 modifier:NSDirectPredicateModifier type:NSContainsPredicateOperatorType options:NSCaseInsensitivePredicateOption]; [searchItemsPredicate addObject:finalPredicate1]; NSCompoundPredicate *resultCompoundPredicate = [NSCompoundPredicateandPredicateWithSubpredicates:searchItemsPredicate]; NSArray *resultAry = [maryfilteredArrayUsingPredicate:resultCompoundPredicate];}#pragma mark 筛选自定义类占位符的使用/* %K:用于动态传入属性名 %@:用于动态设置属性值 */-(void)test5{ NSArray *nameAry =@[@"张三",@"李四" ,@"宋五",@"马留",@"张三",@"李四"]; NSArray *ageAry =@[@8, @18,@28 , @38 ,@48, @58]; NSMutableArray *mary = [NSMutableArrayarray]; for (int i =0; i < nameAry.count; i++) { person *per = [personnew]; per.name = nameAry[i]; per.age = ageAry[i]; [mary addObject:per]; } // 检查出 name是张三, 年龄是 48的对象 NSPredicate *predicate = [NSPredicatepredicateWithFormat:@"name LIKE '张三' && age = 48"]; NSArray *result = [maryfilteredArrayUsingPredicate:predicate]; NSLog(@"%@", result); // 打印结果 "<person: 0x7f98a0e215a0>"// 占位符的使用 NSString *key =@"name"; NSString *value =@"张三"; // 本例的意思是筛选出 person实例中 name是 张三的对象. NSPredicate *predicate1 = [NSPredicatepredicateWithFormat:@"%K CONTAINS %@", key, value]; NSArray *result1 = [maryfilteredArrayUsingPredicate:predicate1]; NSLog(@"%@", result1); // 查找出年龄大于 25岁的对象 NSString *keyAge =@"age"; NSPredicate *predicate2 = [NSPredicatepredicateWithFormat:@"%K > $AGEVAULE", keyAge]; // 给 $VALUE赋值 // NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys: @20, @"AGEVAULE", nil]; NSDictionary *dict = [NSDictionarydictionaryWithObject:@25forKey:@"AGEVAULE"]; NSPredicate *predicate3 = [predicate2 predicateWithSubstitutionVariables:dict]; NSArray *result3 = [maryfilteredArrayUsingPredicate:predicate3]; NSLog(@"result3 %@", result3);}#pragma mark 直接量/* FALSE、NO:代表逻辑假 TRUE、YES:代表逻辑真 NULL、NIL:代表空值 SELF:代表正在被判断的对象自身 "string"或'string':代表字符串 数组:和c中的写法相同,如:{'one', 'two', 'three'}。 数值:包括整型、小数和科学计数法表示的形式 十六进制数:0x开头的数字 八进制:0o开头的数字 二进制:0b开头的数字 */#pragma mark 集合运算符/* ANY、SOME:集合中任意一个元素满足条件,就返回YES。 ALL:集合中所有元素都满足条件,才返回YES。 NONE:集合中没有任何元素满足条件就返回YES。如:NONE person.age < 18,表示person集合中所有元素的age>=18时,才返回YES。 IN:等价于SQL语句中的IN运算符,只有当左边表达式或值出现在右边的集合中才会返回YES。我们通过一个例子来看一下 */-(void)test4{ NSArray *ary =@[@"hello",@"betray",@"resemble",@"declare",@"reservation",@"shink"]; NSArray *ary1 =@[@"betray",@"resemble",@"declare",@"period"]; // 返回目标数组 不在 ary数组中的元素 NSPredicate *predicate = [NSPredicatepredicateWithFormat:@"NOT (SELF IN %@)",ary]; NSArray *result = [ary1filteredArrayUsingPredicate:predicate]; NSLog(@"%@",result); // 打印结果 period}#pragma mark 字符串比较 test3/* BEGINSWITH:检查某个字符串是否以指定的字符串开头(如判断字符串是否以a开头:BEGINSWITH 'a') ENDSWITH:检查某个字符串是否以指定的字符串结尾 CONTAINS:检查某个字符串是否包含指定的字符串 LIKE:检查某个字符串是否匹配指定的字符串模板。其之后可以跟?代表一个字符和*代表任意多个字符两个通配符。比如"name LIKE '*ac*'",这表示name的值中包含ac则返回YES;"name LIKE '?ac*'",表示name的第2、3个字符为ac时返回YES。 MATCHES:检查某个字符串是否匹配指定的正则表达式。虽然正则表达式的执行效率是最低的,但其功能是最强大的,也是我们最常用的。 注:字符串比较都是区分大小写和重音符号的。如:café和cafe是不一样的,Cafe和cafe也是不一样的。如果希望字符串比较运算不区分大小写和重音符号,请在这些运算符后使用[c],[d]选项。其中[c]是不区分大小写,[d]是不区分重音符号,其写在字符串比较运算符之后,比如:name LIKE[cd] 'cafe',那么不论name是cafe、Cafe还是café上面的表达式都会返回YES。 */-(void)test3{ NSString *str =@"Creates and returns a new predicate format by creating a new string with a given format and parsing the result"; // 注意区分大小写,区分 重音符号 NSPredicate *predicate = [NSPredicatepredicateWithFormat:@"SELF BEGINSWITH 'Creates'"]; NSPredicate *prediceta2 = [NSPredicatepredicateWithFormat:@"SELF BEGINSWITH[cd] 'creates'"]; if ([predicateevaluateWithObject:str]){ NSLog(@"str包含Creates "); } if([prediceta2evaluateWithObject:str]){ NSLog(@"str包含Creates 不区分大小写 "); } /* 2016-09-18 10:58:01.911 NSPredicate_谓词的使用[30548:1730520] str包含Creates 2016-09-18 10:58:01.912 NSPredicate_谓词的使用[30548:1730520] str包含Creates 不区分大小写 */ // ======================= /* 1.如果被筛选的string中包含空格,会把string按空格分割,然后将分割的单个元素与条件关键字匹配(本例是 'new'),注意关键字 ('new') 与单个元素完全匹配才行,而不是元素只要包含关键字就行. 2.如果被筛选的string不包含空格,会把string和关键字 (本例是 'new')进行匹配,只要包含就行. */ NSPredicate *predicate3 = [NSPredicatepredicateWithFormat:@"SELF LIKE[cd] '*new*'"]; // 被筛选的字符串中(本例中指的是str)不能包含空格, 否则一直返回NO NSPredicate *predicate4 = [NSPredicatepredicateWithFormat:@"SELF LIKE[cd] '*eates'"]; if ([predicate3evaluateWithObject:str]) { NSLog(@"str包含 new"); } if ([predicate4evaluateWithObject:str]) { NSLog(@"str第一个字母后面是reates");// 这样是检查不到的,因为有空格 只能匹配到 creates }}#pragma mark 逻辑运算符 test2/* AND、&&:逻辑与,要求两个表达式的值都为YES时,结果才为YES。 OR、||:逻辑或,要求其中一个表达式为YES时,结果就是YES NOT、 !:逻辑非,对原有的表达式取反 */-(void)test2{ // 筛选出 ary中 >50 && < 100 的数 NSArray *ary =@[@123,@12,@66,@78,@0,@34]; NSPredicate *predicate = [NSPredicatepredicateWithFormat:@"SELF > 50 && SELF < 100"]; /* 1. 会自动遍历数组. 2. 返回一个符合要求的数据 数组, */ NSArray *result = [aryfilteredArrayUsingPredicate:predicate]; NSLog(@"ary %@", ary); NSLog(@"result %@", result); //================ NSMutableArray *mary = [arymutableCopy]; NSArray *result1 = [maryfilteredArrayUsingPredicate:predicate]; NSLog(@"mary %@", mary); NSLog(@"result1 %@", result1); //========= 打印结果 /* 2016-09-18 10:36:41.201 NSPredicate_谓词的使用[30495:1720212] ary ( 123, 12, 66, 78, 0, 34 ) 2016-09-18 10:36:41.201 NSPredicate_谓词的使用[30495:1720212] result ( 66, 78 ) 2016-09-18 10:36:41.201 NSPredicate_谓词的使用[30495:1720212] mary ( 123, 12, 66, 78, 0, 34 ) 2016-09-18 10:36:41.201 NSPredicate_谓词的使用[30495:1720212] result1 ( 66, 78 ) */} 比较运算 test1/* >=,=>:判断左边表达式的值是否大于或等于右边表达式的值 <=,=<:判断右边表达式的值是否小于或等于右边表达式的值 >:判断左边表达式的值是否大于右边表达式的值 <:判断左边表达式的值是否小于右边表达式的值 !=、<>:判断两个表达式是否不相等 BETWEEN:BETWEEN表达式必须满足表达式 BETWEEN {下限,上限}的格式,要求该表达式必须大于或等于下限,并小于或等于上限 */-(void)test1{ NSNumber *num =@123; // SELF 代表自己 NSPredicate *predicate = [NSPredicatepredicateWithFormat:@"SELF >= 100"]; /* - (BOOL)evaluateWithObject:(nullable id)object; 用一个NSPredicate 评价(评价是我的直译,也可以理解成过滤)一个obj,当满足时返回YES */ if ([predicateevaluateWithObject:num]) { NSLog(@"满足 %@", num); // 打印结果 满足 123 }}@end
0 0
- NSPredicate 的简单介绍
- [Object-c]_[初级]_[数组NSArray过滤NSPredicate的简单介绍]
- NSPredicate的在查询中的简单使用
- UISearchBar, NSPredicate 实现简单的搜索功能
- iOS中 NSPredicate 的简单使用
- NSPredicate谓词过滤的简单使用
- 简单了解一下--谓词(NSPredicate)
- NSPredicate的用法
- NSPredicate的使用
- NSPredicate的用法
- NSPredicate的用法
- NSPredicate的用法
- NSPredicate的用法
- NSPredicate的用法
- NSPredicate的用法
- NSPredicate的用法
- NSPredicate的用法
- NSPredicate的用法
- 网约技师APP详细设计说明书
- dataGridView设置
- ODI数据抽取教程_03定时执行
- HDOJ 3264 Open-air shopping malls 计算几何 二分
- 微服务(Microservice)那点事
- NSPredicate 的简单介绍
- git使用教程一 本地仓库操作
- LaTeX技巧心得255:用laTeX排版大括号的三种方法
- JavaScript单选框复选框
- Eclipse/MyEclipse中安装freemarker编辑器插件
- my 3 c
- 致有志成为程序员的你
- Maven dependenciess jar 包 丢失
- 和社区的issue交流