OC之NSPredicate(谓词)学习
来源:互联网 发布:詹姆斯社交软件头像 编辑:程序博客网 时间:2024/06/05 18:55
NSPredicate(谓词)
谓词:OC中的谓词操作是针对于数组类型的,他就好比数据库中的查询操作,数据源就是数组,这样的好处是我们不需要编写很多代码就可以去操作数组,同时也起到过滤的作用,我们可以编写简单的谓词语句,就可以从数组中过滤出我们想要的数据。非常方便。在Java中是没有这种技术的,但是有开源的框架已经实现了此功能。
实例:
定义一个person类
@interface Person : NSObject@property (nonatomic, copy) NSString *name;@property (nonatomic, assign) int age;+ (instancetype)personWithName:(NSString *)name andAge:(int)age;@end//便利初始化方法,直接初始化一个对象+ (instancetype)personWithName:(NSString *)name andAge:(int)age{ Person *person = [[Person alloc]init]; person.name = name; person.age = age; return person;}- (NSString *)description{ NSString *desc = [NSString stringWithFormat:@"name:%@,age:%d",_name,_age]; return desc;}
- 创建一个数组,用来存放person类的对象
NSArray *personArray = @[ [Person personWithName:@"niuer" andAge:12], [Person personWithName:@"zhangsan" andAge:13], [Person personWithName:@"lisi" andAge:14], [Person personWithName:@"wangwu" andAge:15], [Person personWithName:@"Zhaoliu" andAge:16], [Person personWithName:@"tianqi" andAge:17], ];
- 谓词的基本用法
- 关系运算符 > < =
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self.age > 12 "];NSArray *result = [personArray filteredArrayUsingPredicate:predicate];NSLog(@"%@",result);predicate = [NSPredicate predicateWithFormat:@"self.age < 15"];result = [personArray filteredArrayUsingPredicate:predicate];NSLog(@"%@",result);predicate = [NSPredicate predicateWithFormat:@"self.age = 15"];result = [personArray filteredArrayUsingPredicate:predicate];NSLog(@"%@",result);
逻辑运算符 &&(and) ||(或) predicate = [NSPredicate predicateWithFormat:@"self.age < 17 && self.age > 12"];result = [personArray filteredArrayUsingPredicate:predicate];NSLog(@"%@",result);predicate = [NSPredicate predicateWithFormat:@"self.age > 15 || self.name = 'niuer' "];result = [personArray filteredArrayUsingPredicate:predicate];NSLog(@"%@",result);
- . ANY(至少一个) ALL(所有)
predicate = [NSPredicate predicateWithFormat:@"ALL self.age < 12"];if ([predicate evaluateWithObject:personArray]) { NSLog(@"所有人都小于12");}else{ NSLog(@"所有人都大于12");}predicate = [NSPredicate predicateWithFormat:@"any self.age < 15"];if ([predicate evaluateWithObject:personArray]) { NSLog(@"至少有一个人小于15");}else{ NSLog(@"所有人都大于15");}
- 范围运算符 between(闭区间) in(包含)
predicate = [NSPredicate predicateWithFormat:@"self.age between {13,15}"]; result = [personArray filteredArrayUsingPredicate:predicate]; NSLog(@"%@",result); predicate = [NSPredicate predicateWithFormat:@"self.age in{13,15} || self.name in {'zhangsan','lisi'}"]; result = [personArray filteredArrayUsingPredicate:predicate]; NSLog(@"%@",result);
- 字符串的操作
下面三个指令一般都加[cd] c:不区分大小写,d 不区分重高音 beginswit h:以..开始 endswith :以..结束 contains :包含... predicate = [NSPredicate predicateWithFormat:@"self.name beginswith[cd] 'z' "]; predicate = [NSPredicate predicateWithFormat:@"self.name beginswith[cd] 'z' "]; result = [personArray filteredArrayUsingPredicate:predicate]; NSLog(@"%@",result); predicate = [NSPredicate predicateWithFormat:@"self.name endswith 'i' "]; result = [personArray filteredArrayUsingPredicate:predicate]; NSLog(@"%@",result); predicate = [NSPredicate predicateWithFormat:@"self.name contains 'i' "]; result = [personArray filteredArrayUsingPredicate:predicate]; NSLog(@"%@",result);
- 关系运算符 > < =
0 0
- OC之NSPredicate(谓词)学习
- OC学习篇之---谓词(NSPredicate)
- OC学习篇之---谓词(NSPredicate)
- 【IOS 开发学习总结-OC-32】★OC之foundation 框架——谓词(NSPredicate)
- OC语法--谓词NSPredicate类
- [iOS学习] 谓词NSPredicate使用
- oc中谓词的使用(NSPredicate)
- oc NSPredicate(谓词)的一些基础知识
- Objective-C之谓词NSPredicate
- objective-C之 NSPredicate(谓词)
- 谓词 OC学习
- 谓词NSPredicate
- NSPredicate 谓词
- 谓词NSPredicate
- NSPredicate 谓词
- 谓词NSPredicate
- 谓词(NSPredicate)
- NSPredicate 谓词
- Android很有用的代码片段
- OC手动内存管理-临时对象与事件循环中的自动释放池
- 区域锁(Scoped locking)
- web service 异常
- 【代码篇】JBPM4.4开发流程节点(动态代理实现流程管理业务)
- OC之NSPredicate(谓词)学习
- OC手动内存管理的规则总结
- 用分支定界(branch and bound)法解装箱问题
- 用动态规划方法旅行商问题(TSP问题)
- 确定core文件是由哪个文件产生的方法
- VHDL语法小结
- 一场说走就走的旅行
- 20150113Review
- iOS 开发者必知的 75 个工具(译文)